notification 加 button 按键

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);
		else
			notificationView.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
		// device
		List<RunningAppProcessInfo> 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");
		
	    }
	}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值