API リクエストを作成し、応答で提供されたデータを使用するサービスがアプリにあり、1 時間ごとにサービスを実行するようにスケジュールする方法はありますか ありがとうございます
質問する
3795 次
2 に答える
6
それが私が最後にやった方法です:
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, ProximityAlertService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Log.d("Main",String.valueOf( cal.getTimeInMillis()));
//make the alarm goes off every 10 sec (not exact help to save battery life)
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 10000, pintent);
于 2013-07-25T10:05:56.800 に答える
1
例:
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, LocationPoller.class);
Bundle bundle = new Bundle();
LocationPollerParameter parameter = new LocationPollerParameter(bundle);
parameter.setIntentToBroadcastOnCompletion(new Intent(context, MyStartServiceReceiver.class));
// try GPS and fall back to NETWORK_PROVIDER
parameter.setProviders(new String[] {LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER});
parameter.setTimeout(60000);
i.putExtras(bundle);
PendingIntent pending = PendingIntent.getBroadcast(context, 54321, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
service.cancel(pending);
cal.add(Calendar.SECOND, 30);
//
// Fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), REPEAT_TIME, pending);
于 2013-07-23T12:09:16.230 に答える