安卓 - 当我的应用完成后恢复后台应用



基本上,我正在Chrome中浏览互联网或查看其他应用程序。我收到打开我的应用程序的通知。我做了一些事情,它就完成了。我的应用程序关闭,我回到启动器上。如何让我的应用完成并返回到我之前浏览的任何应用/Chrome 页面?

此活动的 Android 清单部分:

<activity
        android:name=".AcceptRejectActivity"
        android:label="@string/title_activity_accept_reject"
        android:screenOrientation="portrait" >
    </activity>

通知代码:

mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent resultIntent = new Intent(this, AcceptRejectActivity.class);
    resultIntent.putExtra("message","Do you choose to accept this message?");      
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(AcceptRejectActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack //
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent contentIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    String msg = "You have a message"
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_logo)
                    .setContentTitle("Message")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(msg))
                    .setContentText(msg)
                    .setPriority(Notification.PRIORITY_HIGH);
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setAutoCancel(true);
    android.app.Notification note = mBuilder.build();
    note.defaults |= android.app.Notification.DEFAULT_VIBRATE;
    note.defaults |= android.app.Notification.DEFAULT_SOUND;        
    mNotificationManager.notify(0, note);

片段的关闭函数

private void allDone() {
        Log.i(TAG, "The choice has been made");            
        getActivity().finish();
        //NavUtils.navigateUpFromSameTask(getActivity());
    }

嗯,这有点奇怪,但就像一个选项一样,您可以在完成应用程序后打开浏览器。

private void allDone() {
        Log.i(TAG, "The choice has been made");            
        getActivity().finish(); //finish your app!.
        //NavUtils.navigateUpFromSameTask(getActivity());
      try { //Open browser!.
            Uri uri = Uri.parse("googlechrome://navigate?url=");
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        } catch (ActivityNotFoundException e) {
            // Chrome is not installed
        }
}

然后,您将返回到您的 Chrome 会话;)。

我设法解决了我的问题。

安卓清单:

<activity
        android:name=".AcceptRejectActivity"
        android:label="@string/title_activity_accept_reject"
        android:screenOrientation="portrait"
        android:launchMode="singleInstance"
        android:excludeFromRecents="true">
    </activity>

通知代码:更改了挂起意向的创建方式。

mNotificationManager = (NotificationManager)
        this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(this, AcceptRejectActivity.class);
resultIntent.putExtra("message","Do you choose to accept this message?");      
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
String msg = "You have a message"
NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_logo)
                .setContentTitle("Message")
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(msg))
                .setContentText(msg)
                .setPriority(Notification.PRIORITY_HIGH);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
android.app.Notification note = mBuilder.build();
note.defaults |= android.app.Notification.DEFAULT_VIBRATE;
note.defaults |= android.app.Notification.DEFAULT_SOUND;        
mNotificationManager.notify(0, note);

现在,当我完成处理后,我可以调用finish()或getActivity().finish(),它会在单击通知之前将我带回您在任何应用程序/浏览器页面上。

最新更新