Service
とが必要ですAlarmManager
。サービスは、位置の取得とサーバーへの投稿を処理し、AlarmManager
決定した間隔に基づいてサービスを呼び出します。AlarmManager は、Service
大まかに次のように、onCreate
または必要な他の場所で初期化する必要があります。
AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, YourAlarmReceiver.class),PendingIntent.FLAG_CANCEL_CURRENT);
// Use inexact repeating which is easier on battery (system can phase events and not wake at exact times)
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, YOUR_ALARM_TRIGGER_AT_TIME,YOUR_ALARM_INTERVAL, pendingIntent);
YourAlarmReceiver がサービスを開始します
public class YourAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, YourService.class));
}
}
サービスの使用方法については、Android Web サイトhttp://developer.android.com/guide/topics/fundamentals/services.htmlを参照してください。