gpt4 book ai didi

java - 单击通知启动 Activity 时控制后退堆栈

转载 作者:行者123 更新时间:2023-12-02 02:39:39 25 4
gpt4 key购买 nike

用户在应用处于后台时点击通知,应用打开并跳转到 PickUpActivity

NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
int notificationId = (int) System.currentTimeMillis();
Intent picukUpIntent = new Intent(context, PickUpActivity.class);
picukUpIntent.putExtra(MainScreenActivity.ORDER_ID, orderId);
picukUpIntent.putExtra(NOTI_TYPE, 3);
pendingIntent = PendingIntent.getActivity(
context,
notificationId,
picukUpIntent,
PendingIntent.FLAG_ONE_SHOT
);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_mini_logo)
.setContentTitle("Title")
.setContentText(message)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
mNotifyMgr.notify(notificationId, builder.build());

当用户在应用程序处于后台时单击通知时,它会按照我的预期跳转到 PickUpActivity。问题是,当用户单击向上或后退按钮时,它会退出应用程序。我希望当用户单击后退或向上按钮时它跳转到 MainScreenActivity。当用户在应用程序位于前台时单击通知时,效果很好。所以我不想覆盖 PickUpActivity 中的后退和向上按钮行为。有没有办法将 PickUpActivity 的父级设置为 MainScreenActivity我确实在 list 中设置了父项,但它不起作用

<activity
android:name=".screen.rating.PickUpActivity"
android:parentActivityName=".screen.main.MainScreenActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".screen.main.MainScreenActivity" />
</activity>

最佳答案

您需要在应用程序处于后台或前台时识别您的应用程序状态,因此使用以下方法您可以识别您的应用程序状态。

public static boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
return isInBackground;
}

然后简单地将下面一行代码添加到您的后按事件中

 @Override
public void onBackPressed() {
super.onBackPressed();
if(NotificationUtils.isAppIsInBackground(context)){
//open your main screen activity MainScreenActivity.class
}else{
// what you want
}
}

关于java - 单击通知启动 Activity 时控制后退堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45707402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com