1

基本的にインテントをメイン メニュー (singleTop 起動モードに設定) に送信する通知をクリックすると、PendingIntent が起動します。これにより onNewIntent が起動され、必要なデータが読み込まれ、そのデータが読み込まれた別のアクティビティに移動します。残念ながら、アプリケーションが現在閉じている場合、onNewIntent は機能しないため、onCreate から onNewIntent を呼び出して、チェックインを onNewIntent に含める必要がありました。

通知を起動した後、pendingIntent は onCreate が呼び出されるたびに起動することに満足しているように見えますが、消えません。チェックを入れる方法や、使用後に保留中のインテントをクリアする方法がわかりません。私のコードは次のとおりです。

protected void onNewIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    if(extras != null && extras.containsKey("notification")) {
        if(extras.getBoolean("notification", false)) {
            loadData(extras.getString("data"));
            Intent myIntent = new Intent(this, OtherClass.class);
            startActivity(myIntent);
        }
    }
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    onNewIntent(getIntent());
}

//Code snippet of notification setup
notification = new Notification(R.drawable.icon, "Notification Reminder", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationIntent = new Intent(this, MainMenu.class);

Bundle bundle = new Bundle();
contentTitle = "Notification_Title";
bundle.putBoolean("notification", true);
bundle.putString("data", contentTitle.toString());
notificationIntent.putExtras(bundle);
                            notificationIntent.setData((Uri.parse("application://"+SystemClock.elapsedRealtime())));

contentTitle = contentTitle + " click me!";
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
                                PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, notificationText, contentIntent);
4

1 に答える 1

2

そのような PendingIntent.FLAG_ONE_SHOT フラグを使用するだけです:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
于 2011-08-25T07:38:23.570 に答える