gpt4 book ai didi

android - 在 Android 中设置警报管理器

转载 作者:太空狗 更新时间:2023-10-29 15:46:29 24 4
gpt4 key购买 nike

我正在按照 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 ));
}

list :

   <uses-permission android:name="android.permission.WAKE_LOCK" />

但是我没有收到任何错误,但警报(Toast 消息)不会触发。

EDIT 来自开发者文档:

RTC 示例

这里有一些使用 RTC_WAKEUP 的例子。

大约在下午 2:00 唤醒设备以触发警报,并且每天同一时间重复一次:

// 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);

最佳答案

确定您的警报需要多精确

选择警报类型通常是创建警报的第一步。另一个区别是您需要警报的精确度。对于大多数应用程序,setInexactRepeating() 是正确的选择。当您使用此方法时,Android 会同步多个不精确重复的警报并同时触发它们。这减少了电池的消耗。

对于像您这样具有严格时间要求的罕见应用,闹钟需要在下午 4:00 精确触发。每天然后使用 setRepeating()

引用:Decide how precise your alarm needs to be

解决方案:

    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);

编辑测试: (每 10 秒触发一次警报)

        alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(),
1000*10, alarmIntent);

结论:

setup() 处理告警前没有调用方法


API 19+ 更新

针对 API 级别 19 或更高级别时,setRepeating 不准确。对于精确重复,您现在可以使用 setExact() 并自行管理重复。

引用:AlarmManager documentation

关于android - 在 Android 中设置警报管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21231721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com