工作内容:
1.通知栏制作全过程(含多个PendingIntent,即:可跳转到多个activity)
2.可产生isChecked的组件ToggleButton可添加setOnCheckedChangeListener事件,处理checked事件
学习分享:
1.通知栏制作过程
第一步:初始化
//添加通知栏信息 public void startNotification(){ //初始化NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); //生成意图intent对象 intentHome = new Intent(this,HomeActivity.class); // intentHome = new Intent("azsecuer.androidy.com.mobelmanager.activity.HomeActivity"); intentYellowPage = new Intent(this,YellowPageActivity.class); /** * 对于intent生成2个对应的pendingintent * pendingIntentHome,pendingIntentYellowPage最后一个参数可以为0,不影响 */ pendingIntentHome = PendingIntent.getActivity(this,0,intentHome,PendingIntent.FLAG_CANCEL_CURRENT); pendingIntentYellowPage = PendingIntent.getActivity(this,0,intentYellowPage,PendingIntent.FLAG_CANCEL_CURRENT); //初始化RemoteViews的对象 remoteViews = new RemoteViews(this.getPackageName(),R.layout.layout_notificationbar); /** * 添加监听事件 * 参数一:package * 参数二:pendingIntent */ remoteViews.setOnClickPendingIntent(R.id.linear_notificationbar,pendingIntentHome); remoteViews.setOnClickPendingIntent(R.id.image_yellowpage_notificationbar,pendingIntentYellowPage); //初始化notification notification = new Notification.Builder(this) .setTicker("手机管家有新消息") //必要条件,提示信息 // .setContentTitle("手机管家") //标题 // .setContentText("点击进入手机管家主页面") //内容 .setSmallIcon(R.drawable.ic_launcher) //必要条件,提示图标 // .setContentIntent(pendingIntentHome) // .setAutoCancel(true) //设置notification可以自动消失 .setContent(remoteViews) .build(); //Android版本在11-16之间用.getNotification(); // notification.flags |= Notification.FLAG_AUTO_CANCEL;//使通知栏可以自动消失——无效 // notification.flags = Notification.FLAG_NO_CLEAR;//如果不调用manager.cancel,通知栏不会消失 }
第二步:调用方法生成通知栏
参数NOTID是一个自定义int常量manager.notify(NOTID,notification);
第三步:调用方法使通知栏消失
manager.cancel(NOTID);
2.实现OnCheckedChangeListener,可以处理ToggleButton事件
![]()