我正在关注Android开发人员文档以及其他一些创建警报管理器的工作,该管理器每天下午4点触发并唤醒CPU,以下是我的代码:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
BroadcastReceiver br;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 16);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
public void setup() {
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
//Invoke the service here Put the wake lock and initiate bind service
}
};
registerReceiver(br, new IntentFilter("com.testrtc") );
alarmIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.testrtc"),
0 );
alarmMgr = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
}
}
清单:
<uses-permission android:name="android.permission.WAKE_LOCK" />
然而,我没有得到任何错误,但警报(Toast消息)不会触发。
来自开发者文档的编辑:
RTC示例
以下是使用RTC_WAKEUP的一些示例。
唤醒设备以在下午2点左右发出警报,并同时每天重复一次:
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
这个用于设置重复,说如果我希望我的闹钟在8:30开火然后在每20分钟后重复,但是我只想在特定时间发出警报但我不想重复它。
唤醒设备,准时上午8:30准时发出警报,此后每20分钟发出一次警报:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 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,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
答案 0 :(得分:3)
确定警报需要的准确程度
选择警报类型通常是创建警报的第一步。另一个区别是您需要警报的准确程度。对于大多数应用,setInexactRepeating()
是正确的选择。当您使用此方法时, Android会同步多个不准确的重复警报并同时触发它们。这样可以减少电池的消耗。
对于像您这样严格的时间要求的罕见应用,闹钟需要在下午4点点亮精确。然后每天使用 setRepeating()
。
参考:Decide how precise your alarm needs to be
<强> Solution :
强>
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
// Set the alarm to start at approximately 4:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 16);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000*60*60*24, alarmIntent);
编辑测试: (Fire alarm at every 10seconds)
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(),
1000*10, alarmIntent);
结论:
在处理警报之前未调用 setup()
方法。
setRepeating是不精确的。对于完全重复,您现在可以使用setExact()
并自行管理重复。
答案 1 :(得分:2)
我建议您使用setInexactRepeating()
setRepeating()
来自文档,
setInexactRepeating(int type,long triggerAtMillis,long intervalMillis,PendingIntent operation)
setRepeating(int type,long triggerAtMillis,long intervalMillis,PendingIntent operation)
答案 2 :(得分:2)
文档很清楚:
public void setInexactRepeating (...)
triggerAtMill是警报首先应以毫秒为单位的时间 关闭,使用适当的时钟(取决于报警类型)。这个 不精确:此时间之前警报不会发出警报,但可能存在 在第一次调用之前几乎整个警报间隔的延迟 警报。
所以我的理解但是可能会延迟几乎整个警报间隔意味着你可能有一天的延迟,因为你使用AlarmManager.INTERVAL_DAY。
答案 3 :(得分:2)
将分钟和秒设置为日历..
calendar.set(Calendar.HOUR, 16); // At the hour you wanna fire
calendar.set(Calendar.MINUTE, 0); // Particular minute
calendar.set(Calendar.SECOND, 0);
并使用
alarmManager.setRepeating(AlarmManager.RTC,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmIntent);
答案 4 :(得分:0)
我有同样的问题(通常是华为问题),我通过在PowerManager或受保护的应用程序中启用应用程序来解决。
答案 5 :(得分:0)
尝试创建一个扩展WakefulBroadcastReceiver的接收器:
public class MyReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myService = new Intent(context, MyService.class);
context.startService(myService);
}
}
在您的服务中,您可以放置吐司,或尝试在文件中写入日志以确保其有效。然后,在你的活动中:
Intent myAlarm = new Intent(context.getApplicationContext(), MyReceiver.class);
PendingIntent recurringAlarm = PendingIntent.getBroadcast(context.getApplicationContext(), 0, myAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), 4 * 1000 * 60, recurringAlarm);
//每4分钟报警