1

電話の充電器が切断されたときに音を鳴らすアプリがあります。すべてがうまく機能しますがACTION_POWER_DISCONNECTED、充電器がACTION_POWER_CONNECTED.

OS がそのようなことをする理由は理解できますが、これは再起動状態によるもので、サウンドが再生されないことを知ることは可能ですか?

これは Nexus 4 で発生し、他のデバイスはテストしていません。

    @Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction() == Intent.ACTION_POWER_CONNECTED) {

    } else if (intent.getAction() == Intent.ACTION_POWER_DISCONNECTED) {
              //Don't want to get there if the phone is rebooting!
    }
}

ありがとう!

4

1 に答える 1

0

私の問題の解決策を考え出しました。これは、充電器の意図が呼び出されたときにデバイスが起動しないようにするための一般的で簡単な方法です。

ACTION_BOOT_COMPLETED(マニフェストの許可と意図を忘れないでください)をキャッチし、共有設定でタイムスタントを設定するだけです。

if (intent.getAction() == Intent.ACTION_BOOT_COMPLETED) {
        SharedPreferences.Editor editor = PreferenceManager
                .getDefaultSharedPreferences(context)
                .edit();
        editor.putLong("uptime", System.currentTimeMillis());
        editor.commit();
        return;
    }

次に、ブート シーケンスに入っていないことを確認するために、システムの稼働時間と比較する単純な条件でコードを囲みます。

    if (preferences.getLong("uptime", System.currentTimeMillis()) > System.currentTimeMillis() - SystemClock.uptimeMillis()) {
               //Your code to be executed there!
    }

これが誰にも役立つことを願っています!

于 2013-09-18T17:39:21.280 に答える