android Notification和NotificationManager的使用

Notification和NotificationManager

1.Broadcast Receiver组件没有提供可视化的界面来显示广播信息。这里我们可以使用Notification和NotificationManager来实现可视化的信息显示。通过使用它们我们可以显示广播信息的内容,图标

以及震动等信息。

2.使用Notification和NotificationManager也比较简单,一般获得系统级的服务NotificationManager,然后实例化Notification,设置其属性,通过NotificationManager发出通知就可以了。

基本步骤如下:

  1)获得系统级的服务NotificationManager,这里比较简单,通过Context.getSystemService()方法即可实现。

1 String service = Notification;
2 NotificationManager nm = (NotificationManager)getSystemService(service);

  2)实例化Notification对象,并设置其属性。

//实例化Notification
Notification n = new Notification();
//谁知显示图标,图标会在状态栏显示
int icon = n.icon = R.drawable.icon;
//设置显示提示信息,
String tickrtText = "Test Notification";
//显示时间
long when = System.currentTimeMills();
n.icon = icon;
n.tickerText = tickerText;
n.when = when;

  3)调用setLatestEcentInfo()方法在视图中设置图标和时间

  //实例化Intent

Intent intent = new Intent(this,MainActivity.class);
//获得 PendingIntent
PendingIntent  pi = PendingIntent  .getActivity(this,0,intent,0);
//设置时间信息
n.setLastEventInfo(this,"My content" , "My Content" , pi);

  4)发出通知

//表示该通知的ID
int ID = 1;
//发出通知
nm.notify(ID,n);

3.下面直接通过一个例子来说明

 

 

 

 

 

 

 

android Notification和NotificationManager的使用,布布扣,bubuko.com

android Notification和NotificationManager的使用

上一篇:Android Studio常用快捷键、Android Studio快捷键大全


下一篇:android 布局中 layout_gravity、gravity、orientation、layout_weight【转】