0

Serviceトラックを再生するためのを作成した音楽プレーヤーアプリを作成しています。

私が使用しているプレーヤーを制御するためにbindService()

以下はコードです。ServiceControllerクラスを使用してbind(inside inistService())およびunbind(inside releaseService())を実行しています。

public class ServiceController {

    MusicServiceAidl aidlObject;
    CallMService serviceConnection = new CallMService();
    Context context;

    public ServiceController(Context c) {
        this.context = c;
    }

    class CallMService implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder boundService) {
            aidlObject = MusicServiceAidl.Stub
                    .asInterface((IBinder) boundService);
            Toast.makeText(context, "Service Connected",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName paramComponentName) {
            aidlObject = null;
            Toast.makeText(context, "Service Disconnected",
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void initService() {
        try {

            Intent serviceIntent = new Intent();
            serviceIntent.setClassName("com.example.async",
                    com.example.async.PlayTrack.class.getName());
            boolean ret = context.bindService(serviceIntent, serviceConnection,
                    Context.BIND_AUTO_CREATE);
            Toast.makeText(context, "Service bound with " + ret,
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(context,
                    "initService(): " + e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    public void releaseService() {
        context.unbindService(serviceConnection);
        serviceConnection = null;
        Toast.makeText(context, "Service Released",
                Toast.LENGTH_SHORT).show();
    }

    public String getTrackName() throws RemoteException{
        return aidlObject.getTrackName();
    }

    public String getAlbumName() throws RemoteException{
        return aidlObject.getAlbumName();
    }

    public String getArtistName() throws RemoteException{
        return aidlObject.getArtistName();
    }

    public void playPreviousTrack() throws RemoteException{
        aidlObject.playPreviousTrack();
    }

    public void playNextTrack() throws RemoteException{
        aidlObject.playNextTrack();
    }

}

このバインディングクラスを呼び出すために、私は使用しています:

ServiceController serviceController = new ServiceController(getApplicationContext());
serviceController.initService();
serviceController.releaseService();

問題は、別のクラスからサービスを停止しようとしていることです。つまり、別のクラスから呼び出したいのですreleaseService。しかし、明らかに、それは与えIllegalArgumentExceptionます。

編集: 以下のコードを実行すると:

public void onBackPressed() {
    try {
        Intent intent = new Intent(this,
                Class.forName("com.example.async.PlayTrack"));
        stopService(intent);
        serviceController.releaseService();
    } catch (Exception e) {
        Log.e("Listing.java", e.toString());
        Toast.makeText(Listing.this,
                "Listing->onBackPressed: " + e.toString(), Toast.LENGTH_SHORT)
                .show();
    }
}

次の例外が発生します

java.lang.IllegalArgumentException: Service not registered: com.example.async.classes.ServiceController$CallMService@40547298

どうすればこれを達成できますか?

4

1 に答える 1

0

アクティビティ コンテキストではなく、アプリケーション コンテキストを使用してバインドおよびバインド解除します。を使用して、アクティビティでアプリケーション コンテキストを取得できますgetApplicationContext()

于 2012-08-14T09:03:36.003 に答える