6

我有一个服务“GroupsTaskAlarmChecker”,AlarmManager 在 Groups.class 活动的 onCreate 中每 20 秒调用一次:

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:

<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 :

 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 里面我不知道我怎么能像以前一样做(意图和 alarmManager 每 20 秒启动一次服务)。此行中的错误:

 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

有可能我不能在 BroadcastReceiver 中创建一个 AlarmManager 吗?

谢谢大家,我是一名 Android 初学者,我需要你们的帮助。对不起我的英语不好 ;)

4

4 回答 4

4

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

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

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

于 2013-08-06T12:42:42.523 回答
2

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

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

于 2013-03-05T16:11:12.640 回答
2

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

首先:在您的应用程序的 AndroidManifest 中配置一个“接收器”。

<receiver
    android:name=".AlarmBroadcastReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

第二:使用扩展抽象类广播接收器的类,您应该确定意图操作是否为“BOOT_COMPLETED”。如果满足条件,您可以从您的类中调用一个方法,该方法具有警报的所有构造。

请参阅下面的代码片段。

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

    }

}
于 2016-12-06T01:20:44.660 回答
0

在您的 onReceive 中:

if ("android.intent.action.BOOT_COMPLETED".equals (intent.getAction())){

   //start it again here

}
于 2013-03-05T10:54:31.157 回答