Android 如何使用安卓AlarmManager,比如1分钟?
我想做一些外部服务监视器,并尽快收到问题通知 我尝试设置Android 如何使用安卓AlarmManager,比如1分钟?,android,timer,alarmmanager,background-service,Android,Timer,Alarmmanager,Background Service,我想做一些外部服务监视器,并尽快收到问题通知 我尝试设置AlarmManager,设置间隔为1-2分钟,但它似乎每隔几分钟随机启动一次 当然,我希望安卓不会杀死我的后台任务,如果我只使用服务,安卓会停止监控 是否可以以小而准确的间隔使用AlarmManager Facebook、Gmail等应用程序使用哪些方法通知新消息 使用startForeground和部分WakeLock进行服务是否更好 我尝试设置AlarmManager,设置间隔为1-2分钟,但看起来它每隔几分钟随机启动一次 由于您决定
AlarmManager
,设置间隔为1-2分钟,但它似乎每隔几分钟随机启动一次
当然,我希望安卓不会杀死我的后台任务,如果我只使用服务
,安卓会停止监控
是否可以以小而准确的间隔使用AlarmManager
Facebook、Gmail等应用程序使用哪些方法通知新消息
使用startForeground
和部分WakeLock
进行服务
是否更好
我尝试设置AlarmManager,设置间隔为1-2分钟,但看起来它每隔几分钟随机启动一次
由于您决定不展示如何“每隔1-2分钟设置AlarmManager”,因此任何人都很难帮助您。请阅读并注意Android 4.4新增的默认不精确行为
我想避免被安卓系统杀死我的后台任务,若我只使用安卓系统,安卓系统将停止监控
AlarmManager
并不能解决这方面的所有问题。例如,如果用户选择强制停止你的应用程序(例如,通过设置),你的警报将被删除
是否可以以小而准确的间隔使用AlarmManager
在Android 1.0-4.3上使用setRepeating()
,在Android 4.4+上使用setExact()
。使用setExact()
,作为处理一个报警事件的一部分,您需要安排下一个报警事件
使用startForeground和部分WakeLock进行服务是否更好
仅当您的设备始终接通电源时(例如,工业过程监视器)。发布我们使用的代码片段。请根据您的要求进行修改。我们每15秒使用一次,效果很好
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver
{
private static final int _REFRESH_INTERVAL = 60 * 1; // 1 minutes
// Alarm id
private static final int ALARM_ID = 102; // This can be any random integer.
PendingIntent pi = null;
AlarmManager am= null;
@Override
public void onReceive(Context context, Intent intent)
{
/* All actions to be handled here.. */
}
// This is to initialize the alarmmanager and the pending intent. It is done in separate method because, the same alarmmanager and
// pending intent instance should be used for setting and cancelling the alarm.
public void SetContext(Context context)
{
this.context = context;
am=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
pi = PendingIntent.getBroadcast(context, ALARM_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
// Setting the alarm to call onRecieve every _REFRESH_INTERVAL seconds
public void SetAlarm()
{
// am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * _REFRESH_INTERVAL , pi);
try
{
am.cancel(pi);
}catch (Exception ignored){}
am.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), 1000 * _REFRESH_INTERVAL , pi);
}
// Cancel the alarm.
public void CancelAlarm()
{
am.cancel(pi);
}
}
如果您将目标版本设置为低于4.4,则不需要单独担心4.4和更高版本。@Eu.Dr:正确。但是,选择
targetSdkVersion
涉及许多因素。精明的程序员考虑到,总有一天,他们需要提高自己的targetSdkVersion
,并忘记它对报警的影响。因此,在开发新的AlarmManager
代码时,这些程序员将选择使用setExact()
,在这些代码中需要精确的报警targetSdkVersion
主要是为了简化现有代码的转换,而不是作为在编写新代码时忽略更改的许可证。我会接受这一点,但我根本无法实现1分钟的间隔,即使在adroid 3 target SDK上也是如此。这可能与我使用的是miuiandroid有关,看起来有一些内部限制,比如5分钟的最小警报interval@killer_PL:我使用了AlarmManager
来演示低至5秒的设置,因此它肯定能够做到这一点。但是AlarmManager
是设备制造商为延长电池寿命而进行更改的领域,因此,如果您的设备是问题的根源,我也不会感到惊讶。您可能会与x86 emulator、其他设备或其他设备进行比较。setRepeating()
在某些设备上无法在Android 5或更高版本上可靠工作,即使您将targetSdk
设置为8(Android 2.2)。大多数较新的设备似乎忽略了这方面的targetSdk
设置,并使用自己的电源管理算法。不幸的是,在我的例子中,这是每5分钟一次的firig。这个解决方案和我的一样——它每5分钟一次。注意两件事:1)从API 19开始,所有警报都是不精确的;2) 为了长时间工作,广播接收器必须在清单中注册,而不是动态注册
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
Intent intent = new Intent(MainActivity.this, YourClass.class);
PendingIntent pintent = PendingIntent.getService(MainActivity.this,
0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
60* 1000, pintent);