首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在AlarmManager触发后调用AlarmManager方法?

如何在AlarmManager触发后调用AlarmManager方法?
EN

Stack Overflow用户
提问于 2016-09-15 09:44:32
回答 1查看 407关注 0票数 2

我想在Intent的每一个火焰中额外的改变AlarmManager。这有可能吗,我如何在AlarmManager一开始就调用它呢?

代码:

代码语言:javascript
复制
public void startCollector(){
    final int LOOP_REQUEST_CODE = 4;
    Intent i = new Intent(getApplicationContext(), DataCollector.class);
    PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),LOOP_REQUEST_CODE,i,PendingIntent.FLAG_NO_CREATE);
    long firstTime = SystemClock.elapsedRealtime();
    firstTime += 3*1000;
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    //TO CHANGE INTENT EXTRAS DO NOT REMOVE.
    if(sender != null){
        am.cancel(sender);
    }
    if(getLocation() != null) {
        i.putExtra("JLocation", getLocation());
    }
    i.putExtra("JLocation",getLocation());
    sender = PendingIntent.getBroadcast(getApplicationContext(),LOOP_REQUEST_CODE,i,PendingIntent.FLAG_CANCEL_CURRENT);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 100000, sender);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-15 15:18:10

我建议你使用一个确切的警报,而不是使用setRepeating(),并重新安排它每次它开火。

(setRepeating()是不准确的,也不适用于Doze )

例如:

代码语言:javascript
复制
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

// if you would like to fire the next alarm very precisely
// put this value in the Intent and use it in your receiver
// to calculate the next time for the alarm
long fireAt = SystemClock.elapsedRealtime() + 5000;

if(Build.VERSION.SDK_INT >= 23) {
    am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                fireAt, pendingIntent);
} else if(Build.VERSION.SDK_INT >= 19) {
    am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, fireAt, pendingIntent);
} else {
    am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, fireAt, pendingIntent);
}

在你的接收器里:

代码语言:javascript
复制
@Override
public void onReceive(Context context, Intent intent) {

    // change the Intent as you wish
    intent.putExtra("anExtra", "aValue");
    // or create a new one
    //Intent newIntent = new Intent(context, this.getClass());

    PendingIntent pendingIntent = PendingIntent
        .getBroadcast(context, 0, intent, 0);

    AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);

    long fireAt = SystemClock.elapsedRealtime() + 5000;

    if(Build.VERSION.SDK_INT >= 23) {
        am.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                fireAt, pendingIntent);
    } else if(Build.VERSION.SDK_INT >= 19) {
        am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, fireAt, pendingIntent);
    } else {
        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, fireAt, pendingIntent);
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39507905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档