2017-01-09 7 views
0

私はAndroidに音楽プレーヤーを書き込もうとしており、バックグラウンドで無限ループで音楽を再生するサービスを利用しようとしていました。ボリュームコントロールなどの活動とやりとりするサービスが必要です。問題は、サービスバインダーを使用すると、そのアクティビティがバックグラウンドにあるときにサービスがアクティビティで終了する可能性が高いことです。サービスがアクティビティなしで実行され続けることを確認し、アクティビティでサービスのメソッドのいくつかを呼び出すことができるかどうかを確認する方法サービスが強制終了されないようにする方法

バインダーを使用してフォアグラウンドサービスを使用していますか?このサービスはこのように殺されませんか?

ありがとうございます!

+0

private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { // We've bound to MusicPlayerService, cast the IBinder and get MusicPlayerService instance LocalBinder binder = (LocalBinder) service; mService = binder.getService(); mBound = true; } @Override public void onServiceDisconnected(ComponentName arg0) { mBound = false; } }; 

を作成します。 stopSelf()メソッドまたはstopService()メソッドを使用してサービスを終了するまで、サービスは永久に実行されます。 – Noorul

答えて

1

this process priority blog postによると、フォアグラウンドサービスよりも優先度が高いのはフォアグラウンドアクティビティだけです。これは、フォアグラウンドサービスがほとんど決して殺されないことを意味します。

building an audio app service documentationパー

サービスが再生されている、それはフォアグラウンドで実行されなければなりません。これにより、システムはサービスが有用な機能を実行していることを知ることができます。また、システムのメモリが不足している場合は、そのサービスを強制終了しないでください。フォアグラウンドサービスは通知を表示して、ユーザーがそれを知っていてオプションでそれを制御できるようにする必要があります。 onPlay()コールバックは、サービスをフォアグラウンドに置く必要があります。

0

注:

は、あなたのサービスが実行またはメソッドの下に使用されていませんチェックstopSelf()またはstopService()メソッドを使用してサービスを殺すまではサービスが生涯に実行されます。このドキュメントアクティビティクラスのdocs

public class MusicPlayerService extends Service { 
// Binder given to clients 
private final IBinder mBinder = new LocalBinder(); 


/** 
* Class used for the client Binder. Because we know this service always 
* runs in the same process as its clients, we don't need to deal with IPC. 
*/ 
public class LocalBinder extends Binder { 
    MusicPlayerService getService() { 
     // Return this instance of MusicPlayerService so clients can call public methods 
     return MusicPlayerService.this; 
    } 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 

/** method for clients */ 
public void play() { 

} 
public void pause() { 

} 
public void stop() { 

} 

} 

バインドservice.checkをcrerateする必要が音楽プレーヤーアプリの

public static boolean isServiceRunning(String serviceClassName){ 
    final ActivityManager activityManager = (ActivityManager)Application.getContext().getSystemService(Context.ACTIVITY_SERVICE); 
    final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE); 

    for (RunningServiceInfo runningServiceInfo : services) { 
     if (runningServiceInfo.service.getClassName().equals(serviceClassName)){ 
      return true; 
     } 
    } 
    return false; 
} 

、 宣言mBoundブール変数、

boolean mBound; 

はbindServiceImplementation

を追加
@Override 
protected void onStart() { 
    super.onStart(); 
    // Bind to MusicPlayerService 
    Intent intent = new Intent(this, MusicPlayerService.class); 
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
} 
@Override 
protected void onStop() { 
    super.onStop(); 
    // Unbind from the service 
    if (mBound) { 
     unbindService(mConnection); 
     mBound = false; 
    } 
} 

サービスのあなたのコードを投稿し、その後、あなたは以下のようなパブリックメソッドMusicPlayerServiceを呼び出すことができ、ServiceConnectionオブジェクトを初期化し、MusicPlayerServiceクラスと通信を行うようにするには

public void onButtonClick(View v) { 
    if (mBound) { 

     // However, if this call were something that might hang, then this request should 
     // occur in a separate thread to avoid slowing down the activity performance. 
     mService.play(); 
     mService.pause(); 
     mService.stop(); 

    } 
} 
+0

サービスを活動と結びつけ、活動が殺害された場合、サービスは終了されます。 –

+0

はい。あなたは正しいです。ドキュメントに基づいて、onDestroy()メソッドでサービスをバインド解除することができます。このドキュメントのリンクを確認してくださいhttps://developer.android.com/guide/components/bound-services.html#Additional_Notes – Noorul

+0

ありがとうございます!私は、活動が殺された後でも、サービスが実行されるようにしたい。 –

関連する問題