android 通知栏

一、拿到NotificationManager manager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

二、创建Notification notification = new Notification(R.drawable.icon,"This is ticket text",System.currentTimeMillis());

第一个参数用于指定通知图标。第二个参数用于指定通知的ticker内容,当通知刚被创建时,会在状态栏一闪而过。第三个参数指定通知创建时间,当下拉系统状态栏时,这个时间会显示在相应的通知上。

三、指定通知的实际标题和内容。notification.setLatestEventInfo(context,"This is content title","This is content text",null);

四、发出通知manager.notify(1,notification);//第一个参数表示通知id不能重复

五、点击通知跳转到NotificationActivity,使用延时执行的intent,即PendingIntent

Intent intent = new Intent(this,Notification.class);

PendingIntent pi = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);

notification.setLatestEventInfo(context,"This is content title","This is content text",pi);//第四个参数

manager.notify(1,notification);

六、打开NotificationActivity时,要关闭通知栏。

public class NotificationActivity extends Activity{

   protected void onCreate(Bundle savedInstanceState){

      super.onCreate(savedInstanceState);

 setContentView(R.layout.notification_layout);

 NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

 manager.cancel(1);//nofify中第一个参数使用的id

   }

}

七、为通知加上声音和振动,LED闪烁

可以直接使用通知的默认效果notification.defaults = Notificaion.DEFAULT_ALL;

Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg"));

notification.sound = soundUri;

long[] vibrates = {0,1000,1000,1000};//静止时长,振动时长,静止时长……

notification.vibrate = vibrate;

notification.ledARGB = Color.GREEN;

notification.ledOnMS = 1000;//亮起时长

notification.ledOffMS = 1000;//暗去时长

notification.flags = Notificaion.FLAG_SHOW_LIGHTS;

猜你喜欢

转载自smartblack.iteye.com/blog/2308458