通知栏打开应用

本文探讨了在Android应用中优化通知栏点击时应用快速切换到前台的问题,通过修改通知栏点击事件处理逻辑,实现了应用从后台快速切换到前台的功能,解决了延迟问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要代码如下:

private void initNotify() {
		manager = (NotificationManager) getActivity().getSystemService(
				Context.NOTIFICATION_SERVICE);
		notification = new Notification(R.drawable.ic_launcher, "title",
				System.currentTimeMillis());
		RemoteViews remoteViews = new RemoteViews(getActivity()
				.getPackageName(), R.layout.music_notification);
		if (isPlay) {
			remoteViews.setImageViewResource(
					R.id.paly_pause_music, R.drawable.pause_blue);
		} else {
			remoteViews.setImageViewResource(
					R.id.paly_pause_music, R.drawable.play_blue);
		}
		Music music = getCurMusic();
		if (music != null) {
			remoteViews.setTextViewText(R.id.title_title, music.getName());
			remoteViews.setTextViewText(R.id.title_album, music.getCategoryName());
		}
		
		Intent openIntent = new Intent();
		openIntent.setAction(Common.MESSAGE_OPEN_APP);
		PendingIntent openpi = PendingIntent.getBroadcast(getActivity(), 0,
				openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteViews.setOnClickPendingIntent(R.id.left_section, openpi);

		Intent closeIntent = new Intent();
		closeIntent.setAction(Common.MESSAGE_CLOSE_APP);
		PendingIntent closepi = PendingIntent.getBroadcast(getActivity(), 0,
				closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteViews.setOnClickPendingIntent(R.id.close, closepi);

		Intent nextIntent = new Intent();
		nextIntent.setAction(Common.MESSAGE_MUSIC_NEXT_EVENT);
		PendingIntent nextPi = PendingIntent.getBroadcast(getActivity(), 0,
				nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteViews.setOnClickPendingIntent(R.id.next_music, nextPi);

		Intent palyIntent = new Intent();
		palyIntent.setAction(Common.MESSAGE_MUSIC_PLAY_PAUSE_EVENT);
		PendingIntent palyPi = PendingIntent.getBroadcast(getActivity(), 0,
				palyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteViews.setOnClickPendingIntent(R.id.paly_pause_music, palyPi);

		notification.flags = Notification.FLAG_ONGOING_EVENT;
		// notification.defaults = Notification.DEFAULT_SOUND;
		notification.sound = null;
		notification.contentView = remoteViews;
		manager.notify(PandaApplication.MUSIC_NOTIFY_BAR, notification);
	}
	
	@SuppressLint("NewApi")
	public void openApp(Context context) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		
		List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(Integer.MAX_VALUE);
		for (RunningTaskInfo runningTaskInfo : runningTaskInfos) {
			if (runningTaskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
				if (isForground(getActivity())) {
					collapseStatusBar(getActivity());
					return;
				}
				if (Integer.valueOf(Build.VERSION.SDK) >= 11) {
					activityManager.moveTaskToFront(runningTaskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME);
					TipMsgHelper.ShowLMsg(getActivity(), "MOVE_TASK_WITH_HOME");
				} else {
					Intent intent = new Intent(getActivity(), runningTaskInfo.topActivity.getClass());
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					getActivity().startActivity(intent);
				}
				return;
			}
		}
	}
	
	private boolean isForground(Context context) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();
		for (RunningAppProcessInfo appProcess : appProcesses) {
			if (appProcess.processName.equals(context.getPackageName())) {
				/*
				BACKGROUND=400 EMPTY=500 FOREGROUND=100
				GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
				 */
				Log.i(context.getPackageName(), "此appimportace ="
						+ appProcess.importance
						+ ",context.getClass().getName()="
						+ context.getClass().getName());
				if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
					Log.i(context.getPackageName(), "处于后台"
							+ appProcess.processName);
					return false;
				} else {
					Log.i(context.getPackageName(), "处于前台"
							+ appProcess.processName);
					return true;
				}
			}
		}
		return false;
	}
	
	public static void collapseStatusBar(Context context) {
        try {
            Object statusBarManager = context.getSystemService("statusbar");
            Method collapse;

            if (Build.VERSION.SDK_INT <= 16) {
                collapse = statusBarManager.getClass().getMethod("collapse");
            } else {
                collapse = statusBarManager.getClass().getMethod("collapsePanels");
            }
            collapse.invoke(statusBarManager);
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }


上述问题,在点击通知栏时,如果应用已经在后台了,切到前台会有延迟,可能是广播延迟的原因。

修改为:

Intent openIntent = new Intent(Intent.ACTION_MAIN);
		openIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		ComponentName componentName = getTopActivity(getActivity());
		ComponentName cn = null;
		if (componentName != null) {
			cn = new ComponentName(componentName.getPackageName(), componentName.getClassName());
		} else {
			cn = new ComponentName(getActivity().getPackageName(), "com.iflytek.hipanda.view.AnimEntrance");
		}
		openIntent.setComponent(cn);
		openIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		PendingIntent openpi = PendingIntent.getActivity(getActivity(), 0,
				openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteViews.setOnClickPendingIntent(R.id.left_section, openpi);

 private ComponentName getTopActivity(Context context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        
        List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(Integer.MAX_VALUE);
        for (RunningTaskInfo runningTaskInfo : runningTaskInfos) {
            if (runningTaskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
                return runningTaskInfo.topActivity;
            }
        }
        return null;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值