java - 当您的应用程序通过通知打开时,是否可以从打开的位置返回到上一个应用程序?安卓

标签 java android flutter android-intent notifications

是否可以从打开申请的位置返回到之前的申请? 例如,我在应用程序 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/

相关文章:

android - 将 Flutter 连接到真实设备中的本地主机服务器

flutter - 如何在 DropdownMenuItem 上使用 onClick 监听器

java - Java 类 ArrayList 如何返回 Iterator 对象?

java - 检查 ArrayList 是否包含来自另一个 ArrayList 的所有元素

android - 从 linux 在 Android 上启动持久后台进程

android - 在 android 日历中插入事件时出现异常

Android使用数据传输协议(protocol)http或https

android - 此版本中使用了已弃用的 Gradle 功能,使其与 Gradle 7.0 不兼容。 flutter

java - Eclipse 错误“文档中跟随根元素的标记必须格式正确

java - 如何在android中使用JSONObject解析转义的Json