android notification and notificationmanager

来源:互联网 发布:中外标准数据库 编辑:程序博客网 时间:2024/06/06 01:34

一:Notification 和NotificationManager,不多说,贴代码。通知栏的

public void creatSystemNotif(int type,Context context){ 

      //type=0表示系统所给界面类型,type=1表示自定义界面类型

     //FLAG_AUTO_CANCEL          该通知能被状态栏的清除按钮给清除掉  
     //FLAG_NO_CLEAR                 该通知能被状态栏的清除按钮给清除掉  
     //FLAG_ONGOING_EVENT      通知放置在正在运行  
     //FLAG_INSISTENT                通知的音乐效果一直播放  
     //FLAG_AUTO_CANCEL;  自动取消通知栏
     //FLAG_FOREGROUND_SERVICE 前景服务
     //ONLY_ALERT_ONCE  警告一次
     Intent intent=new Intent(this,SecondActivity.class);
     PendingIntent pi=PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);       
 switch(type){
  //系统
 case 0:
       Notification nf0=new Notification(R.drawable.icon, "系统通知栏",1000);
       nf0.setLatestEventInfo(context, "title", "当前的通知栏", pi);  
       //PS:以上的效果常量可以累加,即通过mNotifaction.defaults |=DEFAULT_SOUND   (有些效果只能在真机上才有,比如震动)
       //flags 变化属性
       nf0.flags =Notification.FLAG_INSISTENT;      
       nf0.flags |=Notification.FLAG_NO_CLEAR;       
       //defaults效果
       nf0.defaults =Notification.DEFAULT_VIBRATE;   
       nf0.ledARGB = 0xff00ff00;
       nf0.ledOnMS = 500;
       nf0.ledOffMS = 2000;
       NotificationManager nfm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
       nfm.notify(1, nf0);
       break;
      //自定义
 case 1:
         Notification nf1=new Notification(R.drawable.icon, "自定义Notification",System.currentTimeMillis());
         RemoteViews contentView=new RemoteViews(getPackageName(),R.layout.notification);
         contentView.setTextViewText(R.id.textview1, "Push广告");
         contentView.setTextViewText(R.id.textview2,"通知栏广告");
         nf1.contentView=contentView;
         nf1.contentIntent=pi;      
         NotificationManager nfm1=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
         nfm1.notify("自定义Notification", 1, nf1);
       break;
 }
}
原创粉丝点击