notification 加 button 按键
16lz
2021-01-26
public class NotificationExtend {private Activity context;private final String TAG = "NotificationExtend";public boolean flag = false;RemoteViews notificationView;Notification notification;public NotificationExtend() {}public void setActivity(Activity context) {this.context = context;}public void resetNotification(String msg) {cancelNotification();showNotification_rl(msg);}public void showNotification(String msg) {if (!isAppOnBackground()) {Logger.e("backgroudn");} else {showNotification_rl(msg);}}public void togglePausePlayButton(boolean playing) {if (playing)notificationView.setImageViewResource(R.id.notification_pause,R.drawable.mediacontroller_pause);elsenotificationView.setImageViewResource(R.id.notification_pause,R.drawable.mediacontroller_play);}@SuppressWarnings("deprecation")public void showNotification_rl(String msg) {Logger.e("showNotification_rl " + msg);NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);notification = new Notification(R.drawable.ic_logo, null,System.currentTimeMillis());notificationView = new RemoteViews(context.getPackageName(),R.layout.notification_item);notificationView.setTextViewText(R.id.notification_msg, msg);Intent notificationIntent = new Intent(context, context.getClass());notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);notification.contentView = notificationView;notification.contentIntent = pendingNotificationIntent;notification.flags |= Notification.FLAG_ONGOING_EVENT;notification.flags |= Notification.FLAG_AUTO_CANCEL;Intent switchIntent = new Intent(context, PauseButtonListener.class);PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(context,0, switchIntent, 0);notificationView.setOnClickPendingIntent(R.id.notification_pause,pendingSwitchIntent);notificationManager.notify(TAG, 5, notification);flag = true;}public void cancelNotification() {if (flag) {NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);notificationManager.cancel(TAG, 5);flag = false;}}public boolean isAppOnBackground() {ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);// Returns a list of application processes that are running on the// deviceList appProcesses = activityManager.getRunningAppProcesses();String packageName = context.getPackageName();if (appProcesses == null || packageName == null)return false;for (RunningAppProcessInfo appProcess : appProcesses) {// The name of the process that this object is associated with.if (appProcess.processName.equals(packageName)&& ((appProcess.importance == RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE))) {return true;}}return false;}}
public class PauseButtonListener extends BroadcastReceiver {public PauseButtonListener(){} @Override public void onReceive(Context context, Intent intent) { Logger.e("test"); }}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer