Android - Intent管理.如果用户从任务管理器打开应用程序,则重新发送旧意图

Sta*_*lav 10 android android-intent

我有个问题.当我调用finish()方法活动时仍保留在任务管理器中,如果用户从任务管理器重新启动它,我的活动会收到旧意图.如果该意图是从推送通知发送的,我会有不必要的反应:我的应用程序通过推送通知数据启动流程意图.

如何正确管理我的活动中的推送通知意图行为以避免错误的活动状态?

我的应用程序会收到推送通知,并表示对推送反应的未决意图:

       final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        int defaultOptions = Notification.DEFAULT_LIGHTS;
        defaultOptions |= Notification.DEFAULT_SOUND;

        Intent intentAction = new Intent(context, MainActivity.class);
        intentAction.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intentAction.putExtra(BUNDLE_COLLAPSE_KEY, data.getString(BUNDLE_COLLAPSE_KEY));
        intentAction.setAction(CUSTOM_ACTION);

        final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);
        int notificationFlags = Notification.FLAG_AUTO_CANCEL;
        final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon_splash)
                .setContentTitle(context.getResources().getString(R.string.app_name))
                .setContentText(data.getString(BUNDLE_PUSH_CONTENT_DATA))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setDefaults(defaultOptions)
                .getNotification();
        notification.flags |= notificationFlags;
        mNotificationManager.notify(NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)

用户从推送,应用程序进入应用程序后,显然,接收意图CUSTOM_ACTION和做一些工作:

private void intentProcess(Intent intent) {
    boolean customAction = intent.getAction().equals(GCMPushReceiver.CUSTOM_ACTION);
    if (customAction) {
        //push reaction, do some staff with intent
    } else {
        //no push reaction, user just open activity
    }
}
Run Code Online (Sandbox Code Playgroud)

我把intentProcess方法从onCreate来自onNewIntent:

public class MainActivity extends FragmentActivity {
//this case if my app closed and user tap on push
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        /* ... */
        intentProcess(getIntent());
    }
//this case if my app opened and user tap on push
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        intentProcess(intent);
    }
}
Run Code Online (Sandbox Code Playgroud)

清单中的活动声明:

    <activity
        android:name=".ui.activity.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

San*_*ngh 1

尝试使用这个

int id= NotificationID.getID();
final PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);

// rest of your code 

notificationManager.notify(id, notification);
Run Code Online (Sandbox Code Playgroud)

添加新班级

    public class NotificationID {

    /**
     * An AtomicInteger is used in applications such as atomically incremented
     * counters, and cannot be used as a replacement for an Integer. However,
     * this class does extend Number to allow uniform access by tools and
     * utilities that deal with numerically-based classes.
     */
    private final static AtomicInteger c = new AtomicInteger(0);

    /**
     * Method to the automatically incremented int value.
     * 
     * @return
     */
    public static int getID() {
        return c.incrementAndGet();
    }
}
Run Code Online (Sandbox Code Playgroud)

每次生成通知时,它都会创建具有不同 id 的新意图。

如果您不希望 Activity 显示/添加到任务管理器,请在 AndroidManifest.xml 的 Activity 标记中添加这些行。

android:excludeFromRecents="true"
android:noHistory="true"
Run Code Online (Sandbox Code Playgroud)