如何在单独的进程中调用服务中的方法?

时间:2015-04-15 07:53:46

标签: android android-activity service

我知道如何通过来自活动的aidl接口调用服务中的方法。但是如何在没有广播接收器的情况下从单独运行的服务中调用活动中的方法?

有什么办法可以通过相同的aidl接口或其他java接口调用我的活动中的方法吗?

代码:

//aidl interface
interface IRemoteServiceCallback {

    void valueChanged();
}

//starting service in activity
Intent serviceIntent = new Intent(BackgroundService.class.getName());
serviceIntent.setPackage("com.example.service2");
startService(serviceIntent);
bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);


//aidl stub implementation in activity
private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {

    @Override
    public void valueChanged() {

        System.out.println("Callback method called");
    }
};

//service connection in activity
BackgroundService mService = null;
private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {

        System.out.println("Callback service connected");
        try {

            mService.registerCallback(mCallback);
        } catch (Exception e) {

            Log.e("Service2-CallbackService-Connecting:", e.toString());
        }
    }

    public void onServiceDisconnected(ComponentName className) {

        if (mService != null) {
            try {
                mService.unregisterCallback(mCallback);
            } catch (Exception e) {
                Log.e("Service2-CallbackService:", e.toString());
            }
        }
    }
};

// registering callbacks in service
public void registerCallback(IRemoteServiceCallback mCallback) {

    System.out.println("Callback registers...");
    this.mCallback = mCallback;
}

public void unregisterCallback(IRemoteServiceCallback mCallback2) {

    this.mCallback = null;
}

//calling method
mCallback.valueChanged();

1 个答案:

答案 0 :(得分:2)

http://developer.android.com/guide/components/aidl.html

您可以通过服务连接注册的接口使用回调。

/** 
     * This implementation is used to receive callbacks from the remote 
     * service. 
     */ 
    private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {
        /** 
         * This is called by the remote service regularly to tell us about 
         * new values.  Note that IPC calls are dispatched through a thread 
         * pool running in each process, so the code executing here will 
         * NOT be running in our main thread like most other things -- so, 
         * to update the UI, we need to use a Handler to hop over there. 
         */ 
        public void valueChanged(int value) {
            mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0));
        } 
    }; 

    private static final int BUMP_MSG = 1;

    private Handler mHandler = new Handler() { 
        @Override public void handleMessage(Message msg) { 
            switch (msg.what) { 
                case BUMP_MSG: 
                    mCallbackText.setText("Received from service: " + msg.arg1); 
                    break; 
                default: 
                    super.handleMessage(msg); 
            } 
        } 

    }; 

//用于注册活动的回调

 private ServiceConnection mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className,
                    IBinder service) {
                // This is called when the connection with the service has been 
                // established, giving us the service object we can use to 
                // interact with the service.  We are communicating with our 
                // service through an IDL interface, so get a client-side 
                // representation of that from the raw service object. 
                ....

                // We want to monitor the service for as long as we are 
                // connected to it. 
                try { 
                    mService.registerCallback(mCallback);
                } catch (RemoteException e) {
                    // In this case the service has crashed before we could even 
                    // do anything with it; we can count on soon being 
                    // disconnected (and then reconnected if it can be restarted) 
                    // so there is no need to do anything here. 
                } 
            } 

            public void onServiceDisconnected(ComponentName className) {
                // This is called when the connection with the service has been 
                // unexpectedly disconnected -- that is, its process crashed. 
               ...
                 if (mService != null) {
                    try { 
                        mService.unregisterCallback(mCallback);
                    } catch (RemoteException e) {
                        // There is nothing special we need to do if the service 
                        // has crashed. 
                    } 
                } 
            } 
        }; 
  

您需要在服务中创建registerCallback和unregisterCallback,   并在需要时调用界面   块引用

//来自服务的代码段

IRemoteServiceCallback mCallback;
    public void registerCallback(IRemoteServiceCallback callback) {
        this.mCallback = callback;
    }

    public void unregisterCallback() {
        this.mCallback = null;
    }
    .
    .
    private void updateActivity() {
        if(mCallback != null) {
            **mCallback.valueChanged(10);**
        }
    }