首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当为BOOT_COMPLETED时,在BroadcastReceiver中使用AlarmManager

当为BOOT_COMPLETED时,在BroadcastReceiver中使用AlarmManager
EN

Stack Overflow用户
提问于 2013-03-05 18:20:47
回答 4查看 4.6K关注 0票数 6

我有一个服务"GroupsTaskAlarmChecker“,由AlarmManager在onCreate of Groups.class活动中每隔20秒调用一次,如下所示:

代码语言:javascript
运行
复制
int seconds = 20;

           Intent myIntent = new Intent(Groups.this, GroupsTaskAlarmChecker.class);
           pendingIntent = PendingIntent.getService(Groups.this, 0, myIntent, 0);

           AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

           Calendar calendar = Calendar.getInstance();
           calendar.setTimeInMillis(System.currentTimeMillis());
           calendar.add(Calendar.SECOND, 10);
           alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), seconds * 1000, pendingIntent);

这个可以完美地工作。但我需要在设备启动时这样做。然后我知道我必须让AndroidManifest像这样:

代码语言:javascript
运行
复制
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <receiver android:name=".ReceiverBoot">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED">
            <category android:name="android.intent.category.HOME">
        </category></action></intent-filter>
    </receiver>

然后像这样的mi broadcastReceiver:

代码语言:javascript
运行
复制
 public class ReceiverBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    int seconds = 20;

        Intent myIntent = new Intent(context, GroupsTaskAlarmChecker.class);
        pendingIntent = PendingIntent.getService(context, 0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 10);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), seconds * 1000, pendingIntent);

    }
}

但在这个onReceive中,我不知道如何才能像以前一样(使用intent和alarmManager每隔20秒启动一次服务)。该行错误:

代码语言:javascript
运行
复制
 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

有没有可能我不能在BroadcastReceiver中创建AlarmManager?

我感谢大家,我是一个机器人乞讨者,我需要你们的帮助。我的英语很抱歉;)

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-03-06 00:11:13

ALARM_SERVICE既没有在类ReceiverBoot中定义,也没有在BroadcastReceiver中定义。

您应该引用Context.ALARM_SERVICE作为getSystemService(String)的参数。

票数 2
EN

Stack Overflow用户

发布于 2013-08-06 20:42:43

总结上面的答案和注释: onReceive处理程序接收可用于访问getSystemService和ALARM_SERVICE的上下文。示例代码:

代码语言:javascript
运行
复制
public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // Start periodic service.
        Calendar cal = Calendar.getInstance();
        Intent srvIntent = new Intent(context, MyService.class);
        PendingIntent pIntent = PendingIntent.getService(context, 0, srvIntent, 0);
        // Use context argument to access service
        AlarmManager alarm = 
                        (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        // Repeat every 5 seconds
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                                5000, pIntent);
        }
    }
}

使用此代码创建一个新类,当然,将MyReceiver和MyService更改为实现中的名称。

票数 4
EN

Stack Overflow用户

发布于 2016-12-06 09:20:45

这里有一点贡献,我相信它可以为实现这个问题的目标添加一个更完整的愿景。

First:在你的应用程序的AndroidManifest内部配置一个“接收器”。

代码语言:javascript
运行
复制
<receiver
    android:name=".AlarmBroadcastReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

Second:使用扩展抽象类BroadcastReceiver的类,您应该确定意图操作是否为"BOOT_COMPLETED“。如果满足条件,您可以从您的类中调用一个方法,该方法具有对您的警报的所有构造。

请看下面的代码片段。

代码语言:javascript
运行
复制
public class AlarmBroadcastReceiver extends BroadcastReceiver {

    String TAG   = "ALARMS";
    String CLASS = this.getClass().getSimpleName() + ": ";

    Context alarmContext;

    @Override
    public void onReceive(final Context context, Intent intent) {

        Log.d(TAG, CLASS + "[START] onReceive()... ");

        alarmContext = context;

        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Log.d(TAG, CLASS + "BOOT_COMPLETED action has been received.");
            setAlarmOnBoot();
        }

            Log.d(TAG, CLASS + "[END] onReceive()... ");

    }

    public void setAlarmOnBoot() {

        Log.d(TAG, CLASS + "[START] - setAlarmOnBoot()");

        final long beginAt  = SystemClock.elapsedRealtime() + 60 * 1000;
        final long interval = 300000; // 5 minutes

        try {
            AlarmManager alarm    = (AlarmManager)  alarmContext.getSystemService(Context.ALARM_SERVICE);
            Intent intent         = new Intent(alarmContext, AlarmBroadcastReceiver.class);
            PendingIntent pIntent = PendingIntent.getService(alarmContext, 0, intent, 0);
            alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, beginAt, interval, pIntent);
            Log.d(TAG, CLASS + "the Alarm has been configured successfully (5 minutes) of interval.");
        } catch (Exception e) {
            Log.d(TAG, CLASS + "an exception has ocurred while setting the Alarm...");
            e.printStackTrace();
        }  

        Log.d(TAG, CLASS + "[END] - setAlarmOnBoot()");

    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15221295

复制
相关文章

相似问题

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