6

ServiceにバインドしたいAndroid がいくつかActivityあるので、ユーザーからのいくつかのアクションを監視できます。

ServiceConnectionすべての Service をバインドできるようにするには、次のようにアクティビティにいくつかの private が必要ですか?

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        GPSLocalBinder gpsBinder = (GPSLocalBinder) service;
        PhotoLocalBinder photoBinder = (PhotoLocalBinder) service;
        gpsService = gpsBinder.getService();
        photoService = photoBinder.getService();
        mGpsBound = true;
        mPhotoBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mGpsBound = false;
        mPhotoBound = false;
    }
};

それとも、アクティビティとサービスの間に、制限されたサービスの使用と理解を向上させるマネージャー クラスが必要ですか?

4

2 に答える 2

0

単一のServiceConnectionインスタンスを使用して、複数のサービスにバインドできます。

ではServiceConnection.onServiceConnected()、どのサービスがバインドされているか (className.getClassName()またはを使用してclassName.getPackageName()) を確認し、それを適切なフィールド/変数などに配置する必要があります。

このスレッドを参考にしましたが、必要に応じて変更しました。

private static final int SERVICE_1_INDEX = 0;
private static final int SERVICE_2_INDEX = 1;

/** Array of the subclasses of {@link BaseService}s which have been bound */
private BaseService[] mServices;
/** ServiceConnection which handles the binding/unbinding of the services */
private MyServiceConnection mServiceConnection;

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mServiceConnection = new MyServiceConnection();
}

@Override
public void onResume() {
    super.onResume();
    bindServices();
}

@Override
public void onPause() {
    super.onPause();
    unbindServices();
}

private void bindServices() {
    Intent service1Intent = new Intent(getActivity().getApplicationContext(), MyService1.class);
    Intent service2Intent = new Intent(getActivity().getApplicationContext(), MyService2.class);

    getContext().bindService(service1Intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    getContext().bindService(service2Intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}

private void unbindServices() {
    if (mServiceConnection != null) {
        getContext().unbindService(mServiceConnection);
    }
}

private class MyServiceConnection implements ServiceConnection {
    public void onServiceConnected(ComponentName className, IBinder boundService ) {
        Log.d("className(bound)", className.getClassName());
        Log.d("className(Service1)", MyService1.class.getName());
        Log.d("className(Service2)", MyService2.class.getName());
        BaseService.LocalBinder binder = (BaseService.LocalBinder) boundService;
        if (className.getClassName().equals(MyService1.class.getName())) {
            mServices[SERVICE_1_INDEX] = binder.getService();
            // call method on your service like:
            // binder.getService().someMethod();
            // (you may need to cast to your actual Service)
        }
        else {
            mBaseServices[SERVICE_2_INDEX] = binder.getService();
            // call method on the service like in if-block
        }
    }

    public void onServiceDisconnected(ComponentName className) {
        if (className.getClassName().equals(MyService1.class.getName())) {
            mBaseServices[SERVICE_1_INDEX] = null;
        }
        else {
            mBaseServices[SERVICE_2_INDEX] = null;
        }
    }
}
于 2016-10-31T11:17:21.347 に答える