在我的应用程序中,我使用以下代码为每周警报启动一项活动:
Manifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".utils.AlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
设置闹钟的类有以下代码:
Intent alarmIntent = new Intent(getActivity(), AlarmReceiver.class);
alarmIntent.putExtra(Constants.HOME_FRAGMENT.CHANNEL_NAME, channelName);
pendingIntent = PendingIntent.getBroadcast(getActivity(), Integer.parseInt(id), alarmIntent, 0);
manager.set(AlarmManager.ELAPSED_REALTIME, calendar.getTimeInMillis(), pendingIntent);
// manager.setRepeating(AlarmManager.ELAPSED_REALTIME, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY*7 , pendingIntent);
接收器类是
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String channelName = intent.getStringExtra(Constants.HOME_FRAGMENT.CHANNEL_NAME);
Intent i = new Intent(context, AlarmLiveNowActivity.class);
i.putExtra(Constants.HOME_FRAGMENT.CHANNEL_NAME, channelName);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(i);
}
}
我的问题是,AlarmLiveNowActivity在设备启动时启动。我不想要这个。它应该在设置警报时启动。我尝试了RTC,RTC_WAKE_UP等所有的ManagerTypes。我也尝试过使用setRepeating报警。但是,在设备启动后,Activity仍会亮起并闪耀!