5 使用Notification实现状态通知栏的通知
5-1 认识Notification (03:38)
一、什么是notification通知栏通知:
notifictioin是显示在手机状态栏的消息,代表一种全局效果的通知
二、通知栏的内容
图标 标题 内容 时间 点击后响应
三、如何实现通知栏
1、通知管理类
获取NotificationManager
显示通知栏:notify(id, notification);
取消通知栏:cancle(id);
2、通知类
构造Notification并设置显示内容
通知栏通知可以设置声音提示、指示灯,以及震动效果
5-2 设置通知参数 (10:17)
一、實現 OnClickListener接口
public class MainActivity extends Activity implements OnClickListener{
二、构造notification并发送到通知栏 /** * 构造notification并发送到通知栏*/ private void sendNotification(){ Intent intent = new Intent(this,MainActivity.class); PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0); Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher);//设置图标 builder.setTicker("hello");//手机状态栏的提示; builder.setWhen(System.currentTimeMillis());//设置时间 builder.setContentTitle("通知栏通知");//设置标题 builder.setContentText("我来自NotificationDemo");//设置通知内容 builder.setContentIntent(pintent);//点击后的意图 //builder.setDefaults(Notification.DEFAULT_SOUND);//设置提 示声音 //builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯 //builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动 builder.setDefaults(Notification.DEFAULT_ALL);//三种效果都有 Notification notifiction = builder.build();//4.1及以上 //builder.getNotification();//4.1以下 *注: 指示灯和震动的设置需要在AndroidManifest.xml中设置权限 users Permission android.permission.FLASHLIGHT android.permission.VIBRATE
pendingIntent字面意义:等待的,未决定的Intent。
要得到一个pendingIntent对象,使用方法类的静态方法
getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int)
分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。
pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。
主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等等。
5-3 发送取消通知 (04:56)
1.NotificationManager manager;//通知控制类 2.manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//初始化 3.定義 notification_ID; 4.manager.notify(notification_ID, notification); 5.manager.cancel(notification_ID);//取消notification
总结:
设置通知参数步骤(转):
第一步:创建Builder对象(是notification的builder)并new出Notification.Builder(this),通过调用builder的方法来设置,setSmallIcon(R.drawable...),setTicker...;
第二步(点击后的响应):创建PendingIntent对象并赋值为PendingIntent.getActivity(context,requestCode,intent,flags):
context:this;
requestCode:请求码,0;
intent:创建Intent对象,在new中根据需求选择构造的类.class;
flags--0;
第三步:创建Notification对象,并将builder.build()赋值//4.1即以上,要用builder.build()方法,以下要用builder.getNotification()方法;
第四步:创建NotificationManager对象,因为是系统的常用服务,赋值为getSystemService(Context.NOTIFICATION_SERVICE),需强制转化;调用成员函数notify(id,notification)来加载Notification,id是一个int值,表示notification的id,自行赋值即可;
Notification 需要用Builder来创建,用Manager来管理所以步骤就是,先创建Buider,然后通过Builder的build()方法来创建 Notification,再通过BuilderManger的notify()方法来管理开启,通过cancel方法来取消;
Notification
1. 创建一个notification
(1)Notification类
new Notification.Builder()可以创建一个Builder对象,通过Builder对象可以设置notification的属性。 Builder builder = new Notification.Builder(this); builder.setSmallIcon(); //设置图标 builder.setTicker(); //手机状态栏的提示,只会显示一会 builder.setWhen(); //设置时间 builder.setContentText(); //设置通知内容 builder.setContentTitle(); //设置标题 builder.setDefaults(Notification.DEFAULT_SOUND); //设置提示音
(2)通过Builder对象的build()可以创建一个notification对象
Notification build = builder.build(); //android4.1以上
(3)设置点击后的意图
Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); builder.setContentIntent(pendingIntent); //设置点击后的意图
2. 设置notification的显示(NotificationManager类)
(1)创建NotificationManager的对象
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
(2)显示notification
notificationManager.notify(notification_id, build);
两个参数notification_id是一个int,build是Builder.build()生成的Notification对象
完整代码如下:
activity_main.xml
<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" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发送通知" android:id="@+id/btn_send" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消通知" android:id="@+id/btn_cancle" /> </LinearLayout> </RelativeLayout>
MainActivity.java
package com.imooc.notificationdemo; import android.app.Activity; import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { NotificationManager manager;// 通知控制类 int notification_ID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); findViewById(R.id.btn_send).setOnClickListener(this); findViewById(R.id.btn_cancle).setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_send: sendNotification(); break; case R.id.btn_cancle: manager.cancel(notification_ID); break; } } /** * 构造notification并发送到通知栏 */ private void sendNotification() { Intent intent = new Intent(this, MainActivity.class); PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0); Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher);// 设置图标 builder.setTicker("hello");// 手机状态栏的提示; builder.setWhen(System.currentTimeMillis());// 设置时间 builder.setContentTitle("通知栏通知");// 设置标题 builder.setContentText("我来自NotificationDemo");// 设置通知内容 builder.setContentIntent(pintent);// 点击后的意图 // builder.setDefaults(Notification.DEFAULT_SOUND);//设置提示声音 // builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯 // builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动 builder.setDefaults(Notification.DEFAULT_ALL);// 设置震动 Notification notification = builder.build();// 4.1以上 // builder.getNotification(); manager.notify(notification_ID, notification); } }
AndroidManifest.xml
<uses-permission android:name="android.permission.FLASHLIGHT"/> <uses-permission android:name="android.permission.VIBRATE"/>