【问题标题】:Android - Intent manage. Old intent is resend if user open application from task-managerAndroid - 意图管理。如果用户从任务管理器打开应用程序,则重新发送旧意图
【发布时间】:2014-08-18 07:06:36
【问题描述】:

我有问题。当我调用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);

在用户通过推送进入应用程序后,应用程序显然会使用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
    }
}

我从onCreateonNewIntent 调用intentProcess 方法:

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);
    }
}

清单中的活动声明:

    <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>

【问题讨论】:

  • 你找到解决办法了吗?
  • @Shena,我不记得了,很久以前了=) 但是,如果你遇到同样的问题,我可以检查一下。
  • 它已经很旧了,但是你在不同的设备上测试过吗?同样的问题?
  • 我认为这可能会对您有所帮助。 stackoverflow.com/questions/4116110/clearing-intent

标签: android android-intent


【解决方案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);

添加一个新类

    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();
    }
}

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

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

android:excludeFromRecents="true"
android:noHistory="true"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多