我有一个简单的应用程序,当用户切换闹钟设置时, 我的应用程序将安排一个任务在午夜显示一个活动(PopupActivity)。
PopupActivity不是我的应用程序的主要活动。我在一些设备上测试过,发现没有问题。 但是,我的一些用户报告说,当警报触发时,我的应用程序将同时启动PopupActivity和MainActivity。
如何避免触发警报时启动的MainActivity?
以下是我的代码快照:
public void scheduleAlarm() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
Intent intent = new Intent(mContext, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, AlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
AlarmReceiver.java:
@Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent();
newIntent.setClass(context, PopupActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.putExtra(PopupActivity.KEY_MESSAGE, "Popup");
context.startActivity(newIntent);
}
的AndroidManifest.xml:
...
<activity
android:name=".MainActivity"
android:label="@string/APP_NAME"
android:configChanges="orientation|locale"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AlarmReceiver" />
<activity
android:name=".PopupActivity"
android:label="@string/APP_NAME"
android:configChanges="orientation|locale"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
...