简体   繁体   English

从App到Android中的SystemService的通信

[英]Communication from App to a SystemService in Android

I'm essentially trying to create a SystemService in Android. 我实质上是在尝试在Android中创建SystemService。 I read that intercommunication from an app to this service should be handled by a Handler? 我读到从应用程序到该服务的互通应该由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: 为了使我的问题更清楚,想象一下,我有一个带有以下方法定义的Service TestService:

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() 从以上我可以理解,为了实现同步,我们通常将setValue作为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? 怎么样getValue方法可以使用Service实例对该方法进行调用并像我们传统上那样正常处理它? 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. 您不必使用Handler来执行任何操作。 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. 如果创建了描述与服务通信的AIDL文件,那么您就完成了:接下来要做的就是在服务内部实现AIDL方法。 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). 当AIDL由Android“编译器”翻译为Java代码时,所有功能都会被阻塞(客户端将等待服务完成方法)。 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. 您应该查看IBinder上的Google文档,以更好地了解事情。

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

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