首页 > 学院 > 开发设计 > 正文

程序通知栏通知的开发

2019-11-08 00:30:32
字体:
来源:转载
供稿:网友

原理:

请求系统发送一个广播,在本程序接收该广播,并做一系列的操作,属于跨进程的开发

1、点击控件请求系统发送广播

public void doClick(View v) {switch (v.getId()) {case R.id.btn_notification:sendNotification();// 发送通知break;

}

2、在通知栏需要显示的通知样式

/*** 自定义*/PRivate void sendNotificationZidinyi() {NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Builder builder = new Builder(MainActivity.this);RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification_zidinyi);//给RemoteViews添加点击意图Intent activityIntent = new Intent(MainActivity.this,MainActivity.class);PendingIntent pi5 = PendingIntent.getActivity(MainActivity.this, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);views.setOnClickPendingIntent(R.id.btn_startActivity, pi5);//给播放按钮添加点击意图Intent broadIntent = new Intent("ACTION_SEND_BROADCAST");PendingIntent broadCastPi = PendingIntent.getBroadcast(MainActivity.this, 0, broadIntent , Context.BIND_DEBUG_UNBIND);views.setOnClickPendingIntent(R.id.img_player, broadCastPi);builder.setContentTitle("").setTicker("自定义").setSmallIcon(R.drawable.small_pic).setContent(views );Notification notifi = builder.build();manager.notify(555,notifi);}

3、动态注册广播接收者

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);receiver = new PlayerBroadcastReceiver();IntentFilter filter = new IntentFilter();filter.addAction("ACTION_SEND_BROADCAST");registerReceiver(receiver, filter);}

4、写一个清除通知的方法,在其生命周期结束时自行清除

private void clearNotification() {// 单例系统服务,可重复创建NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);manager.cancel(NOTIFI_MANAGER_ID);}

@Overrideprotected void onDestroy() {// TODO Auto-generated method stubisDoing=false;NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);manager.cancel(588);unregisterReceiver(receiver);super.onDestroy();}


上一篇:Git命令行

下一篇:AIDL自动挂断电话

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表