简体   繁体   English

如何在单独的过程中从服务中调用活动中的方法?

[英]How to call method in activity from service in seperate process?

I know how to call methods in service through aidl interface from activity.我知道如何通过活动中的 aidl 接口调用服务中的方法。 But how to call method in activity from service which is running in seperate process, without broadcast receiver ?但是如何在没有广播接收器的情况下从在单独进程中运行的服务调用活动中的方法?

Is there any way i can call methods in my activity through same aidl interface or other java interface ?有什么方法可以通过相同的 aidl 接口或其他 java 接口在我的活动中调用方法吗?

Code:代码:

//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();

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

You can use callbacks through interfaces registered through service connection.您可以通过通过服务连接注册的接口使用回调。

/** 
     * 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); 
            } 
        } 

    }; 

// for registering the callbacks from activity // 用于注册来自活动的回调

 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. 
                    } 
                } 
            } 
        }; 

You need to create registerCallback and unregisterCallback in service, and call the interface when required Blockquote需要在service中创建registerCallback和unregisterCallback,需要时调用接口Blockquote

//Code snippet from Service //来自Service的代码片段

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);**
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM