ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 跨进程 Messenger 通信 ~~~ public class TestServiceActivity { Messenger mMessenger ; public void send() { if(mMessenger != null){ Message message = Message.obtain(null, 444); Bundle data = new Bundle(); data.putString("msg", "hahhaha"); message.setData(data); try { mMessenger.send(message); } catch (RemoteException e) { e.printStackTrace(); } } } ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mMessenger = new Messenger(service); Log.i("wjf", "onServiceConnected IBinder = "+ service); } @Override public void onServiceDisconnected(ComponentName name) { } }; public void bindService(){ Intent intent = new Intent(TestServiceActivity.this, MyService.class); bindService(intent,serviceConnection,BIND_AUTO_CREATE); } ~~~ ~~~ public class MyService extends Service { private static class MyMessageHandle extends Handler{ @Override public void handleMessage(Message msg) { Log.i("MyService","message = " + msg); } } private final Messenger mMessager = new Messenger(new MyMessageHandle()); @Nullable @Override public IBinder onBind(Intent intent) { return mMessager.getBinder(); } } ~~~ ![](https://uploader.shimo.im/f/qpgM8KllEpUsMMH9.png!thumbnail) ## 跨进程 AIDL 通信 //todo ## IntentService IntentService是继承并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统的Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们手动去控制或stopSelf()。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以 ## 参考资料 [Android Service完全解析,关于服务你所需知道的一切(上)](https://blog.csdn.net/guolin_blog/article/details/11952435) [ Android Service完全解析,关于服务你所需知道的一切(上)](https://blog.csdn.net/guolin_blog/article/details/9797169) [Service 详解](https://juejin.cn/post/6857132657055268871) [说一说 Service 的启动流程](https://juejin.cn/post/6857132385067892743) [Service 的绑定原理](https://juejin.cn/post/6923369663489376269)