Notification(通知)
1.概述:
Notification是显示在手机状态栏的通知,代表的是一种具有全局效果的通知,程序一般用NotificationManager服务来发送Notification。(API level 11以后用NotificationManager)
2.用法:
- 步骤:
1>调用getSystemService(NOTIFICATION.SERVICE)方法获取系统的NotificationManager服务。
2>通过构造器创建一个Notification对象。
3>为Notification设置各种属性。
4>通过NotificationManager发送Notification。 - Notification.Builder提供的常用方法:
setAutoCancel(); //设置点击通知后,状态栏自动删除通知
setContentTitle(); //设置通知的标题
setContentText(); //设置通知内容
setSmallcon(); //设置图标
setLargeIcon(); //设置大图标
setSound();//设置声音
setTick();//设置通知在状态栏时显示的提示文字。
setContentIntent();//设置点击通知后将要启动的程序组件对应的PendingIntent.
setDafaults(); //设置通知LED灯、音乐、振动等。
setDafaults()的属性值:
Notification.DEFAULT_LIGHTS:默认闪光灯
Notification.DEFAULT_SOUND默认声音
Notification.DEFAULT_VIBRATE默认振动
ALL:默认声音、振动、闪光灯。
如果不想使用默认,可以使用自定义:
//自定义声音
setSound(Uri.parse(“file:///sdcard/click.mp3”));
//自定义振动
setVibrate(new long[]{0,50,100,150});
发送方式:.notify();
取消方式:.cancel();
- 权限问题:
当需要访问系统额闪光灯、振动器时,需要在AndroidManifest.xml中声明权限:
<!--闪光灯权限-->
<uses-permission android:name="android.permisson.FLASHLIGHT"/>
<!--操作振动器权限-->
<uses-permission android:name="android.permisson.VIBRATE"/>
3.范例:
功能:弹出通知,点击通知,界面会再打开本activity
11版本之前的方法:(了解即可)
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//1.建立manager
Notification notification = new Notification();//2.new一个通知实例
//3设置通知的各个属性,直接赋值,其实并不安全(这是最早的方法,所以这样)
notification.icon=R.drawable.liminhao;//设置图标
notification.tickerText="有一个通知!";//设置来通知时,通知栏的文字。
notification.flags=Notification.FLAG_AUTO_CANCEL;//设置可以取消
notification.when=System.currentTimeMillis();//设置触发的时间,下面一行的方式也可以
//notify.when=Calendar.getInstance().getTimeInMillis();
//4.设置通知的标题内容以及触发的事件:
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
PendingIntent pend = PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(getApplicationContext(), "这是通知的标题", "这是通知的内容", pend);
//5.通知管理器将通知添加
manager.notify(1,notification);
结果演示:
现在的方法:
往往因为版本问题,需要加一句:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
PendingIntent pend = PendingIntent.getActivity(getApplicationContext(), 2, intent,PendingIntent.FLAG_ONE_SHOT);
Notification notification = new Notification.Builder(SecondActivity.this).setSmallIcon(R.drawable.liyifeng).setWhen(System.currentTimeMillis()).setTicker("这是一个通知").setContentTitle("这是通知的标题").setContentText("这是通知的内容").setContentInfo("这是通知的内容2").setAutoCancel(true).setShowWhen(true).setContentIntent(pend)
.build();//一定不要落下build方法。(16版本以上用build)
manager.notify(2,notification);
解析:
1)setContentIntent()方法需要传入一个PendingIntent对象。
该对象里封装了一个Intent对象。
2)PendingIntent与Intent的区别:
Intent:是即时传送的。
PendingIntent:是延时的,如果不动它,就一直在。
3)版本的问题:
showwhen()方法在17之后才能用,这之前是setWhen()替代。
build()方法是16以上才能用。
所以一般加上@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
版本声明。
4.自定义通知
1.概述:
利用RemoteVie
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification);
views中可以放的控件:Textview/ImageView/ProgressBar
注:1)也可以放Button,但不响应事件。2)一定不可以放SeekBar
views中可以放的布局:LinearLayout3)一定要设置通知的标题,内容。
2.步骤:
1>new RemoteViews对象。
2>在builder的设置属性中加上setContent(RemoteViews对象)【利用builder方法时:】
注:利用之前的方法时,使用setContentView= ;
范例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonNotify = (Button) findViewById(R.id.btn_notification);
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mButtonNotify.setOnClickListener(new OnClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override
public void onClick(View arg0) {
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(getApplicationContext(),0,intent,PendingIntent.FLAG_ONE_SHOT);
Notification notificatinon = new Notification.Builder(MainActivity.this).setSmallIcon(R.drawable.ic_launcher).setTicker("这是一个通知").setContentText("通知").setContent(views).build();
manager.notify(0,notificatinon);
}
});
}
结果演示: