【发布时间】: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
}
}
我从onCreate 和onNewIntent 调用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