0

使ってみた

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleWithFixedDelay(new Runnable(){

            public void run() {
                // update server  (method which has asyncTask)
            }

        }, 0, 600, TimeUnit.SECONDS);

そして、モバイルがスリープすると動作しなくなります。私はalarmManagerを使用して同じことをしたいのですが、上記の要件に対してサービス、ブロードキャストサービス、またはアクティビティを使用する必要があるかどうかわかりません。

私を助けてください。

前もって感謝します。

4

1 に答える 1

1

ネットワーキングを行う予定なので、Serviceを使用することをお勧めします。実行間で状態を保持する必要がない場合、またはスレッドを手動で処理したくない場合は、IntentServiceを使用します。

これは、ポーリング サービスをセットアップするために使用しているコードです。

    Intent intent = new Intent( context, YourService.class );
    PendingIntent pendingIntent = PendingIntent.getService( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT );

    AlarmManager alarmManager = (AlarmManager) context.getSystemService( ALARM_SERVICE );
    alarmManager.setRepeating( AlarmManager.ELAPSED_REALTIME, POLLING_INTERVAL, POLLING_START_DELAY, pendingIntent );
于 2013-02-04T18:03:26.013 に答える