アラームタスクを実行し、ウィジェットを更新し、必要に応じて通知を送信するために、AlarmManagerを実装して電話を1日に1回スリープ状態にしました。Android AlarmManager setRepeatingは長い間隔で繰り返されません
私はアラームが初めて(SystemClock.elapsedRealtime()+60000
) をトリガされますが、それは86400000
ミリ秒(24時間)後にトリガーされませんsetRepeating
とELAPSED_REALTIME_WAKEUP
を使用しています。
本当にこれに苦しんでいます。私は何か間違っていることを受け入れるか、私がやろうとしていることを達成するためのよりよい方法があればうれしく思います。しかし、私のコードは、人々がやるような標準的なもののように見えると思います。
これは、繰り返しアラームがすべての場合にそうすべきことをしないのとほぼ同じです。私が10分と言うように間隔を狭めると、アラームが作動し、サービスが繰り返し実行されます。
私のアプリの性質は、1日に1回以上の更新が過度であることを意味します。 私はこれに対して現実的で信頼できる解決法を見つける必要があります。
ありがとうございます&私は正しい方向に私を指すことができますように願っています。
ここに私のアラーム実装コードは...だ
マニフェスト:ブート放送用
<receiver android:name=".SystemChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
<receiver android:name=".UpdateAlarmReceiver" />
<service android:name=".UpdateService" />
<receiver android:name=".WidgetProviderSmall" android:label="@string/widget_small_label">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_small" />
</receiver>
<receiver android:name=".WidgetProviderLarge" android:label="@string/widget_large_label">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_large" />
</receiver>
SystemChangeReceiver
聴取し、チェックし、それは、それはそれを設定した場合、アラームは、設定する必要がある場合。
SystemChangeReceiver
:
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.prefs_name), 0);
Boolean notifications = prefs.getBoolean("enable_updates", false);
if(notifications == true) {
Utils.setNotificationAlarm(context);
}
}
setNotificationAlarm方法、アラームが私の受信機UpdateAlarmReceiver
は何をすべきかを決定するトリガーとWakefulIntentService
実行に私のバックグラウンド更新を使用している場合...
public static void setNotificationAlarm(Context context) {
AlarmManager alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, UpdateAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.setRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+60000,
86400000,
pi);
}
を繰り返しアラームを設定しますサービスのハンドラは、ウィジェットを更新し、必要に応じて通知を送信します。
UpdateAlarmReceiver
:
public void onReceive(Context context, Intent intent) {
WakefulIntentService.sendWakefulWork(context, UpdateService.class);
}
これをすべて解決する簡単な質問:タスクキラーを使用していますか?あなたのアプリはホワイトリストに載っていますか? –
私は実際にAutoKiller Memory Optimizerを使用していますが、これはホワイトリストを持っていません。プロセスリストによれば、私のアプリはまだ動作しています。 – Rob