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");    }}


更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)PopupWindow 使点击区域外
  2. Android(安卓)Studio 4.0 新功能之Live L
  3. 仿写Arouter框架实现Activity的跳转
  4. Android获取控件的大小
  5. Gradle设置代码混淆
  6. Android(安卓)输入框弹出样式
  7. Android(安卓)自定义titlebar控件(自定义U
  8. 【Android(安卓)Developers Training】 2
  9. 【Android】玩转命令行工具-aapt2
  10. Andorid入门--HTTP数据传输和WebView的使