简体   繁体   中英

Communication from App to a SystemService in Android

I'm essentially trying to create a SystemService in Android. I read that intercommunication from an app to this service should be handled by a Handler?

So what about returning some object from the Service's function to the calling app? How can this be handled.

To make my question more clear, Imagine I have a Service TestService with the following method definitions:

public class TestService extends ITestService.Stub {
public TestService(Context context) {
        super();
        mContext = context;
        mWorker = new TestWorkerThread("TestServiceWorker");
        mWorker.start();
        Log.i(TAG, "Spawned worker thread");
    }
public void setValue(int val) {
        Message msg = Message.obtain();
        msg.what = TestWorkerHandler.MESSAGE_SET;
        msg.arg1 = val;
        mHandler.sendMessage(msg);
}

public Object getValue() {
     // ********************* QUESTION HERE *****************
    // Can I call this method directly??
    // Or do I have to process this through the handler?????
}
private class TestWorkerThread extends Thread {
        public TestWorkerThread(String name) {
            super(name);
        }
        public void run() {
            Looper.prepare();
            mHandler = new TestWorkerHandler();
            Looper.loop();
        }
    }
    private class TestWorkerHandler extends Handler {
        private static final int MESSAGE_SET = 0;
        @Override
        public void handleMessage(Message msg) {
            try {
                if (msg.what == MESSAGE_SET) {
                    Log.i(TAG, "set message received: " + msg.arg1);
                }
            } catch (Exception e) {
                // Log, don't crash!
                Log.e(TAG, "Exception in TestWorkerHandler.handleMessage:", e);
            }
        }
    }
}

This is what I understand from the above in order to be synchronous we generally make the setValue to be executed as part of the handleMessage()

What about the getValue method can make a call to this method using the Service instance and process it normally like how we do traditionally? Or do I have to work with the handler which is highly unlikely (I beleive). Kindly let me know the best process to deal with in this scenario.

Thanks

You don't HAVE to use a Handler to do any of that. If you've created an AIDL file that describes the communication with the service then you're done: all you have to do next is implement the AIDL methods inside your service. When AIDL is translated into java code by the Android "compiler", you functions will all be blocking (the client will wait for the service to finish the methods). If you specifically add the keyword 'oneway' then the method call won't be blocking. You should check out Google's documentation on IBinder to understand things a little better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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