Android 添加 通知栏控制组件
通知栏插件需要自动重启权限:
添加方法:
public void updateNotification() { boolean isShow=false; if (SettingInfo.shareInstance().isNotifyPlugin()||MyTrackInfo.getInstance().isStart()){ isShow=true; } if (!isShow){ ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notify_info); ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notify_control); return; } if (mNotificationControl == null) { mNotificationControl = new Notification(R.drawable.ic_launcher, "NotificationName", System.currentTimeMillis()); mNotificationControl.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; RemoteViews contentView = new RemoteViews(mContext.getPackageName(), R.layout.layout_notify_control); mNotificationControl.contentView = contentView; PendingIntent pendingIntent1 = PendingIntent.getBroadcast(mContext, 0, new Intent(NotificationBroadcastReceiver.CLICK_ACTION_BUTTON_1), 0); contentView.setOnClickPendingIntent(R.id.button1, pendingIntent1); PendingIntent pendingIntent2 = PendingIntent.getBroadcast(mContext, 0, new Intent(NotificationBroadcastReceiver.CLICK_ACTION_BUTTON_2), 0); contentView.setOnClickPendingIntent(R.id.button2, pendingIntent2); } if (MyTrackInfo.getInstance().getStatus() == MyTrackInfo.status_create) { mNotificationControl.contentView.setTextViewText(R.id.button1, "开始室内"); mNotificationControl.contentView.setTextViewText(R.id.button2, "开启户外"); } else if (MyTrackInfo.getInstance().getStatus() == MyTrackInfo.status_resume) { mNotificationControl.contentView.setTextViewText(R.id.button1, "暂停"); mNotificationControl.contentView.setTextViewText(R.id.button2, "结束"); } else if (MyTrackInfo.getInstance().getStatus() == MyTrackInfo.status_pause) { mNotificationControl.contentView.setTextViewText(R.id.button1, "继续"); mNotificationControl.contentView.setTextViewText(R.id.button2, "结束"); } if (mNotificationInfo == null) { mNotificationInfo = new Notification(R.drawable.ic_launcher, "notificationName", System.currentTimeMillis()); mNotificationInfo.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; RemoteViews contentView = new RemoteViews(mContext.getPackageName(), R.layout.layout_notify_info); PendingIntent contentIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(NotificationBroadcastReceiver.CLICK_ACTION_BUTTON_OPEN), 0); mNotificationInfo.contentIntent = contentIntent; mNotificationInfo.contentView = contentView; } mNotificationInfo.contentView.setTextViewText(R.id.textView1, TimeUtils.second2Time(13:12:31 00:00:00)); mNotificationInfo.contentView.setTextViewText(R.id.textView2, RunningUtils.getMeterStr(31)); mNotificationInfo.contentView.setTextViewText(R.id.textView3, TimeUtils.second2Pace(3)); if (mNotificationManager == null) { mNotificationManager = (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE); } mNotificationManager.notify(notify_info, mNotificationInfo); mNotificationManager.notify(notify_control, mNotificationControl); //if (totalSec%5==0) { mContext.sendBroadcast(new Intent(ControlAllWidgetProvider.ACTION_CONTROL_UPDATE)); //} }
xml布局文件:
<TextView android:id="@+id/button2" android:background="@drawable/btn_hollow"/>
椭圆形背景:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 3 <solid android:color="#00000000" /> 4 <stroke 5 android:width="1dp" 6 android:color="#808080" /> 7 8 </shape>

浙公网安备 33010602011771号