0

サウンドの有効化/無効化とサウンド バックグラウンドの有効化/無効化という設定を使用したアクティビティがあります。sharepreferences の読み取り mediaplayer を開始するサービスを開始します。アクティビティがonPause()状態になったら、メディアプレーヤーを一時停止する必要があります。

アクティビティ:

// reading sharedpreferences, if true:
    startService(new Intent(this, UnUsedService.class));

サービス:

    private PendingIntent pendingIntent;
    public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    player.start();

};
4

1 に答える 1

1

これらの解決策が正しいかどうかはわかりません。しかし、あなたは間違いなくそれを試すことができます.

A. BindingおよびUnbindingサービスonPause()_onResume()

AcitivityunBindアクティビティでService する必要がありますが、 をonPause()呼び出さなかった場合unbind ()、メモリ リークが発生すると思います。

ActivityreBindアクティビティのサービスを提供する必要がonResume()あります。しかし、これらのバインド解除により、NullPointerException


B. _ Sending BroadCastReceiverあなたにそのようなものを構築するかもしれません。

必要に応じてブロードキャストを登録および登録解除する

Activity-onPause() の内部

String PAUSE_MUSIC="PAUSE_MUSIC_INSIDE_SERVICE";
Intent intent = new Intent(PAUSE_MUSIC_INSIDE_SERVICE);
sendBroadcast(intent);

サービスの内部

BroadCastReceiver receiver=new PauseMusicReceiver(); 
IntentFilter filter = new IntentFilter(Activity.PAUSE_MUSIC);
getApplicationContext().registerReceiver(receiver, filter);

class PauseMusicReceiver extends BroadcastReceiver 
{
  @Override
  public void onReceive(Context context, Intent intent) 
  {
     //Pause your MediaPlayer
   }
}

C. _Sleep or Suspend Thread which is created at Service

Service 内の Media も、Service 用に作成したスレッドで処理されます。そのため、Activity の onPause() でその Thread を一時停止またはスリープ モードにし、Activity の onResume() でその Thread を再開すると役立つ場合があります。

スレッドメソッド -onSuspend()、onStart()、onResume()、onSleep()

于 2013-01-05T15:14:38.903 に答える