是否可以从打开申请的位置返回到之前的申请? 例如,我在应用程序 A 中,然后单击通知并打开应用程序 B,然后在应用程序 B 中单击按钮,然后返回应用程序 A?目前我正在尝试做到这一点,而我实现的唯一方法是当我的应用程序在后台运行或仅在第一次运行时。
我正在使用 Flutter,但这些操作是在 native Android 代码中执行的,所以我可能遗漏了一些东西。
为了返回到我正在使用的上一个应用程序:
moveTaskToBack(true);
为了创建通知,我的代码如下所示:
private void NotifyToAutofill(String uri, String url, NotificationManager notificationManager) {
if (notificationManager == null || isNullOrWhitespace(uri)) {
return;
}
Context context = getApplicationContext();
Intent startIntent = new Intent(context, MainActivity.class);
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startIntent.setAction(Intent.ACTION_RUN);
startIntent.putExtra("uri", uri);
startIntent.putExtra("url", url.contains("\u2022")
|| url.contains("\u2731") ? "" : url);
String channelId = "channel-01";
String channelName = "test";
int importance = NotificationManager.IMPORTANCE_LOW;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
int color = 0x008000;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("Test Service")
.setContentText("Tap to open application")
.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(startIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(AutoFillNotificationId, notification);
}
最佳答案
找到了解决方案。我的问题是我正在使用
创建 PendingIntent stackBuilder.addNextIntent(startIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
当我使用时,一切都按预期进行:
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
关于java - 当您的应用程序通过通知打开时,是否可以从打开的位置返回到上一个应用程序?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59884741/