1

お知らせからランチ活動。

Intent notificationIntent = new Intent(context, MainActivity.class); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
notificationIntent.putExtra(GCM_EXTRA_ID, id);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notificationManager.notify(0, notification); 

アクティビティが破棄された場合、インテントは新しいアクティビティを作成し、oncreate アクティビティでこのコードを呼び出します

Bundle extras = getIntent().getExtras();

if(extras != null){
    if(extras.containsKey(GCMIntentService.GCM_EXTRA_ID))
    {
        int id = extras.getInt(GCMIntentService.GCM_EXTRA_ID);
        Toast.makeText(this, "GCM_EXTRA_ID ACTIVITY ON CREATE:" + Integer.toString(id) , Toast.LENGTH_LONG).show();
        dbClassItem = new DBClass(this);
        db =  dbClassItem.getWritableDatabase();
        DBClass.setLocateValue(db, id);
        db.close();
        getIntent().putExtra(GCMIntentService.GCM_EXTRA_ID, 0);
    }
}

戻るボタンでアクティビティを閉じて、もう一度アクティビティを開始すると、そのインテントにはキーが追加されていますGCMIntentService.GCM_EXTRA_ID (これは公開されている静的文字列です)

次のアプリの起動のために、このエクストラをクリアまたは交換するにはどうすればよいですか? getIntent().removeExtra()getIntent().getExtras().clear(),getIntent().putExtra(GCMIntentService.GCM_EXTRA_ID, 0)動作しません。私に何ができる ?活動には android:launchMode ="singleTop"鍵があります。

4

2 に答える 2

1

この問題は、Activity で onCrete を使用する代わりに onNewIntent を使用することで解決されました。

于 2015-10-19T08:37:37.367 に答える
0

あなたはこれを行うことができます

       notificationIntent.putExtra(GCM_EXTRA_ID, null);
于 2013-12-24T20:14:49.093 に答える