【发布时间】:2020-05-10 02:09:47
【问题描述】:
是否可以从打开您的应用程序的位置返回到以前的应用程序? 例如,我在应用程序 A 中,然后单击通知并打开应用程序 B,然后在应用程序 B 中单击按钮,然后返回应用程序 A?目前我正在尝试这样做,我实现的唯一方法是当我的应用程序在后台运行或者它只第一次工作时。
我正在使用 Flutter,但这些操作是在原生 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);
}
【问题讨论】:
-
是的,如果您的应用不再可用或需要大量内存,则可能会破坏您的应用。
-
不再可用是什么意思?我只是想以编程方式进行,我尝试了 movetaskbackto(true) 和 onBackPressed() 但它仍然无法正常工作
标签: java android flutter android-intent notifications