2

アプリをスリープ状態にしてから、設定した時間に起動したいと考えています。私はそれを眠らせますが、目覚めさせません。

これにより、ウェイクロックが設定されます。

private void setWakeLock(){
    System.out.println("wakelock");
    PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE, "DoNotDimScreen");
    wl.acquire();
}

これにより、起床/睡眠時間のアラームが設定されます。

private void setWakeSleep(){
    java.util.Calendar c = java.util.Calendar.getInstance();
    c.set(java.util.Calendar.HOUR_OF_DAY, 17);
    c.set(java.util.Calendar.MINUTE, 53);
    c.set(java.util.Calendar.MILLISECOND, 0);
    Intent sleepIntent = new Intent("SLEEP_INTENT");
    PendingIntent sleepPendingIntent = PendingIntent.getBroadcast(this, 0, sleepIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), sleepPendingIntent);

    c.set(java.util.Calendar.HOUR_OF_DAY, 18);
    c.set(java.util.Calendar.MINUTE, 14);
    c.set(java.util.Calendar.MILLISECOND, 0);
    Intent wakeIntent = new Intent("WAKE_INTENT");
    PendingIntent wakePendingIntent = PendingIntent.getBroadcast(this, 0, wakeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager2.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), wakePendingIntent);
}

そして、これは放送受信機です:

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Time updateHour = new Time();
        updateHour.set(System.currentTimeMillis());         
        if (intent.getAction().equals("SLEEP_INTENT")) {
            System.out.println("sleep");
            wl.release();
        }
        if (intent.getAction().equals("WAKE_INTENT")) {         
            wl.acquire();
            System.out.println("wake");
            //initialise();
        }
    }
};

どんな助けでも大歓迎です!

4

2 に答える 2

8

まず、ウェイクロックは必要ありません。これらは、デバイスがスリープ状態にならないようにするためのものであり、アプリが本当に必要としない限り、非常に反社会的です (バッテリーを消費します)。

第 2 に、ウェイクアップ時間を設定するコードは、18:14 以降に呼び出すと失敗します。これは、過去の時間を定義しているためです。今はそれを無視しましょう。

次に、衝突を引き起こす可能性のある単なる「WAKE_INTENT」ではなく、「org.user1797190.WAKE_INTENT」のようなインテント アクションにする必要があります。このインテントを公開する予定がある場合は、http://openintents.orgで登録することを検討してください。ただし、それはあなたの問題でもありません。

alarmManager2 は必要ありません。システムにはアラーム マネージャが 1 つしかないため、最初のアラーム マネージャを再利用してください。

アプリ自体を「スリープ」状態にするという話は聞いたことがありません。アプリを削除して、後で戻ってきてほしいということですか?

これが私がすることです。「SLEEP_INTENT」を完全に忘れてください。「WAKE_INTENT」をスケジュールしてから、finish() を呼び出すだけです。アプリは単に画面から離れます。

放送受信機のことは完全に忘れてしまいます。代わりに、getBroadcast() の代わりに getActivity() を使用して、アクティビティを再開する保留中のインテントを取得します。WAKE_INTENT がアクティビティに移動するようにマニフェストを変更します。また、アクティビティの複数のインスタンスが作成されないように、「android:launchMode」プロパティを「singleTask」に設定する必要があります。onNewIntent()また、到着時にアクティビティが既に実行されている場合は、wakeup インテントを処理するために実装する必要があります。

最後に、アクティビティがインテントを作成する同じアプリケーションの一部である場合、名前付きインテントはまったく必要ありません。クラスごとに作成できます。ただし、これがウェイクアップ インテントであることを受信者に知らせる別の方法が必要です。

したがって、すべてをまとめると、次のようになります。

マニフェストには次が含まれている必要があります。

<activity android:name=".TestActivity" android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

コードには以下が含まれている必要があります。

/**
 * Arrange for the activity to return at a specific time.
 * Call finish() after calling this method().
 * This function can be called from anywhere that has a valid Context.
 */
public static void scheduleWakeup(Context ctx, long timeMillis) {
    if (DEBUG) Log.d(TAG, "Scheduling wakeup for " + timeMillis);
    Intent intent = new Intent(ctx, TestActivity.class);
    intent.putExtra("wakeup", true);
    PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent,
                          PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager mgr = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
    mgr.cancel(pi);    // Cancel any previously-scheduled wakeups
    mgr.set(AlarmManager.RTC_WAKEUP, timeMillis, pi);
}

...

protected void onCreate(Bundle state) {
    Intent intent = getIntent();
    if (intent.getBooleanExtra("wakeup", false)) {
        // We were woken up by the alarm manager
    }
    ...
}

protected void onNewIntent(Intent intent) {
    if (intent.getBooleanExtra("wakeup", false)) {
        // We were woken up by the alarm manager, but were already running
    }
}

これは、私が自分のアプリで行っていることと非常によく似ており、私にとっては非常にうまく機能します。

もちろん、これは自分でテストする必要があります。Log.d() はあなたの友達です。

于 2012-12-03T19:11:42.833 に答える
0

上記のように。問題は、呼び出しアクティビティ内でブロードキャスト レシーバーを使用していたことです。

于 2012-12-03T19:52:09.377 に答える