前端
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.wuzuo.notification.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_send"
android:text="发送消息"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_cancel"
android:text="取消消息"/>
</LinearLayout>
</RelativeLayout>
androidanifest.xlm
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wuzuo.notification">
<span style="color:#FF0000;"><uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />获取手机震动响铃的权限</span>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
后端
package com.example.wuzuo.notification;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.renderscript.ScriptGroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
private NotificationManager manager;//构建一个通知栏管理者
private Button button1;
private Button button2;
int notification_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
button1= (Button) findViewById(R.id.button_send);
button2= (Button) findViewById(R.id.button_cancel);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button_send:
sendnotification();
break;
case R.id.button_cancel:
manager.cancel(notification_ID);//取消通知栏
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void sendnotification()
{
Intent intent=new Intent(this,MainActivity.class);//跳转到主页
PendingIntent pintent=PendingIntent.getActivity(this,0,intent,0);//等待未决定的
Notification.Builder builder=new Notification.Builder(this);//创建一个Notification 下的builder工程师builder
builder.setSmallIcon(R.mipmap.ic_launcher);//头像 /*工程师建造了头像,主题,铃声*/
builder.setTicker("Hello");//显示在头栏里面的
builder.setContentTitle("起床啦!");//通知栏里面的主题
builder.setWhen(System.currentTimeMillis());//时间
builder.setContentText("点击可在睡十分钟");//通知栏里的副主题
builder.setDefaults(Notification.DEFAULT_ALL);//震动,响铃
builder.setContentIntent(pintent);
Notification notification=builder.build();//起个名字叫notification
manager.notify(notification_ID,notification);//利用通知栏管理者的notify属性去管理notification
}
}