0

の助けを借りて、特定の間隔でサービスを開始しようとしていBroadcastReceiverます。次のように、レシーバーをサービス自体の内部クラスとして定義しました。

public class AlarmReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("RTESPLDebug", "Recieved Broadcast!"); //Never appears in LogCat
        //Intent i = new Intent(context, LocationUpdateService.class);
        context.startService(intent);
    }
};

サービスのアラームレシーバーを登録しますonCreate()

context = super.getApplicationContext();

AlarmReceiver ar = new AlarmReceiver();
IntentFilter intfilter = new IntentFilter(Intent.ACTION_DEFAULT);
intfilter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(ar, intfilter);

最初のアクティビティからサービスを開始し、そこに 1 つのアラームを登録します。

Intent i = new Intent(context, LocationUpdateService.class);
context.startService(i);

// Schedule first alarm
int nextUpdateInterval = 30*1000; // Let first alarm be after 30 sec
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(context, LocationUpdateService.class);
intent.setAction(Intent.ACTION_DEFAULT);
intent.addCategory(Intent.CATEGORY_DEFAULT);
PendingIntent pintent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+nextUpdateInterval, pintent);

サービスでは、単一の場所の更新を登録し、更新を受信すると (受信します)、それを処理します (これをデバッグしましたが、処理は実際にエラーなしで完了します)。次に、上記と同様の方法で次のアラームを登録します。

int nextUpdateInterval = getNextUpdateInterval(); // Returns 30000
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(context, LocationUpdateService.class);
intent.setAction(Intent.ACTION_DEFAULT);
intent.addCategory(Intent.CATEGORY_DEFAULT);
PendingIntent pintent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+nextUpdateInterval, pintent);

たくさんのコードと長い質問。

4

0 に答える 0