3

Android用のライブ壁紙を開発しています。設定した時間に壁紙を更新するには、AlarmManager を使用します。ほとんどの場合、これはうまく機能しますが、アラームが受信されないことがあります。その上、この動作を再現することはできません。ランダムに発生します。少なくとも 3 つの ROM を使用してこれに遭遇しました。

今コードのために。
私はこの PendingIntent を使用します:

mRefreshIntent = new Intent()
    .setComponent(new ComponentName(mContext, RefreshBroadcastReceiver.class))
    .setAction("my.package.name.REFRESH_WALLPAPER");
mPendingRefreshIntent = PendingIntent.getBroadcast(
    mContext, 
    0, 
    mRefreshIntent, 
    PendingIntent.FLAG_CANCEL_CURRENT);

これは、アラームを設定するための私のコードです:

mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, mPendingRefreshIntent);

ここで、time はミリ秒単位の UTC 時間です。を使用して、アラームが意図したとおりに設定されているかどうかをよく確認しましたadb shell dumpsys alarm

受信側:

public class RefreshBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("DayNight", "onReceive     ; " + System.currentTimeMillis());
        DayNightService.refresher.refresh();
        Log.d("DayNight", "onReceive done; " + System.currentTimeMillis());
    }
}

関連するマニフェスト行:

<application>
    ...
    <receiver
        android:name="RefreshBroadcastReceiver">
        <intent-filter>
            <action android:name="my.package.name.REFRESH_WALLPAPER" />
        </intent-filter>
    </receiver>
    ...
</application>

起動されないアラームは常に事前にキュー (dumpsys アラーム) に存在し、その後のアラーム ログには存在しません。Tマイナスゼロで「迷子」になるようです。

あなたの一人が私のためにこの問題を解決できるなら、私はとても幸せです.

4

1 に答える 1

2

次のコードを使用します。

  Intent intent = new Intent(ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
    Log.d(LOG_TAG, "pending intent: " + pendingIntent);
    // if no intent there, schedule it ASAP
    if (pendingIntent == null) {
        pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        // schedule new alarm in 15 minutes
        alarmService.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(),300000, pendingIntent);
        Log.d(LOG_TAG, "scheduled intent: " + pendingIntent);
    }

不正確な繰り返しアラームと RTC ( RTC_WAKEUP ではない) を要求することに注意してください。電話がジーンズのポケットの奥深くで眠っている場合、ユーザーはライブ壁紙の変更に興味がありません。バッテリー ジュースを無駄にして電話を起こす必要はありません。

再起動時に更新スケジュールを開始するには、ブート完了ブロードキャスト レシーバーを登録する必要がある場合もあります。

于 2011-12-09T15:11:54.430 に答える