我试图每5分钟举杯一次,播放一首警戒歌曲,但它没有效果,什么也没发生。
清单:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
..
<receiver android:name=".AlarmReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
MainActivity:
alarmMgr = (AlarmManager)MainActivity.this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 5 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 5, alarmIntent);
AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "OnReceive alarm test", Toast.LENGTH_SHORT).show();
}
}
如果缺少某些东西或有什么错误的任何想法?
答案 0 :(得分:2)
setRepeating
在功能上已弃用(自api 19开始),而推荐使用setInexactRepeating
,该间隔至少为15分钟,并且在批处理多个警报时可能会增加额外的延迟。
如果您想要这样的激进警报,则需要使用一次setExact
警报,然后重新安排Object.values(data)
中的下一个警报。