2
我有一個鬧鐘應用程序,這是使用報警管理器和廣播接收器。 該應用程序是一個單一的活動和4個片段。當警報關閉時,onReceive方法向主活動發送意圖,主活動在onNewIntent方法中接收此意圖,然後移動到正確的碎片。一切正常,除非在應用程序關閉後鬧鐘響起。onNewIntent沒有呼籲重啓
一旦我銷燬應用程序,鬧鐘仍會關閉,並且廣播接收器的意圖會被觸發,但onNewIntent方法確實會捕獲該意圖並將應用程序移動到正確的片段。
這裏是在移動到主要活動
Intent alarmIntent = new Intent(context, ClockActivity.class);
alarmIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmIntent.putExtra("Alarm Name", receivedAlarm.getmName());
context.startActivity(alarmIntent);
在這裏,廣播接收器類的意圖是在我的主要活動我onNewIntent方法,當警報被稱爲應用程序時,是沒有得到所謂的關閉了。
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
PhraseFragment phraseFragment = new PhraseFragment();
String activeName = intent.getStringExtra("Alarm Name");
Bundle args = new Bundle();
args.putString("activeName", activeName);
phraseFragment.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.container, phraseFragment)
.addToBackStack("phrase")
.commit();
}
請參見: http://stackoverflow.com/questions/8800006/onnewintent-is-not-called-in-android-tabs – grebulon