基本的にインテントを(シングルトップの起動モードに設定されている)メインメニューに送信する通知をクリックすると、PendingIntentが発生します。これにより、onNewIntentが起動し、必要なデータがロードされ、そのデータがロードされた別のアクティビティに移動します。残念ながら、アプリケーションが現在閉じている場合、onNewIntentは機能しません。そのため、onCreateからonNewIntentへの呼び出しを行い、onNewIntentにチェックを入れる必要がありました。アクティビティの再ロード時にPendingIntentが起動し続けます
通知が発生した後を除いて、すべてがうまく機能します.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);
私は私のメインメニューに新しいテントをオフに焼成することにより、この問題を修正しました。これはまあまあのようだが、私が望んでいたデータをロードして、ユーザーが戻るボタンを使ってメインメニューに戻るときに、それを開いたままにしておきたいという活動のために働く。私の変更されたonNewIntentは単に "startActivity(新しいIntent(this、MainMenu.class));" startActivity(myIntent);の直前。 – Nemph