【问题标题】:When your application is opened from notification is it possible to get back to previous application from where it was opened? Android当您的应用程序从通知打开时,是否可以从打开它的位置返回到以前的应用程序?安卓
【发布时间】: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


【解决方案1】:

找到了解决办法。我的问题是我正在用

创建 PendingIntent
        stackBuilder.addNextIntent(startIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
        );

当我使用时,一切都按预期工作:

        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2014-10-29
    相关资源
    最近更新 更多