我有一個服務「GroupsTaskAlarmChecker」是這種方式稱爲每20秒AlarmManager中的onCreate Groups.class活動:AlarmManager內廣播接收器時BOOT_COMPLETED
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>
然後英里廣播接收器是這樣的:
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成員,我需要你的幫助。對不起,我的英語;在你的onReceive)
我忘了在這個問題中寫這行,但沒有寫在代碼@Jacob中。和接收廣播的init工程(在我測試init活動和它的ok之前),我只想知道如何寫入我寫入方法onReceive的第一個代碼。這可能嗎? – carlosdiazp 2013-03-05 10:31:51