2

座標をサーバーに取得する GPS サービスがあります。このサービスは、24 時間年中無休で実行されることを前提としています。しかし、それはどういうわけかその間に殺されます。これは、android v 2.3 でのみ発生しています。Android v2.2では問題なく動作しています。

このサービスでは、「LocationManager」を使用しています。これは、ループを作成しているメソッド「requestLocationUpdates」です。このループは、座標のフェッチを担当します。したがって、私の目標は、ループを実行し続けることです。

では、サービスを 24 時間年中無休で実行するにはどうすればよいでしょうか。

4

2 に答える 2

2
This server suppose to be run 24/7

そんなことはできません。あなたが発見したように、これは用語の本当の意味では不可能です。それはまた、まったく良いデザインの選択ではありません。

常に実行する必要がある場合は、PARTIAL_WAKE_LOCKviaを取得する必要がありますPowerManager。これにより、CPU が常にオンになり、プログラムが実行されます。バッテリー寿命の衝撃的な低下に備えてください。

代わりにAlarmManagerを使用してください。AlarmManager を介してPendingIntentをスケジュールし、関連する時点でサービスを開始してその作業を行うことができます。完了したら、サービスを再度終了します。

以下は、今から 5 分後に YourService を開始するインテントを起動する AlarmManager の使用方法を示すサンプル コードです。

// get a calendar with the current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 5);

Intent intent = new Intent(ctx, YourService.class);
PendingIntent pi = PendingIntent.getService(this, 123, intent, 
                                            PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
于 2012-08-13T07:07:37.260 に答える
0

一定時間後にサービスを開始する繰り返しアラームマネージャーを使用してください

private void setAlarm() {
        AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(getApplicationContext(), LocationUpdateService.class);
        intent.putExtra("locationSendingAlarm", true);
        PendingIntent   pendingIntent = PendingIntent.getService(this, AppConstants.PENDING_INTENET_LOCATION_SENDING_ALARM_ID, intent,0);
        try {
            alarmManager.cancel(pendingIntent);
        } catch (Exception e) {

        }
        int timeForAlarm=5*1000*60; // 5 minutes


        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+timeForAlarm, timeForAlarm,pendingIntent);
    }
于 2012-08-13T07:04:16.707 に答える