设为首页 加入收藏

TOP

Bound Service的三种方式(Binder、 Messenger、 AIDL)(四)
2015-07-20 17:28:22 来源: 作者: 【 】 浏览:17
Tags:Bound Service 方式 Binder Messenger AIDL
port android.os.RemoteException; import android.util.Log; import android.view.View; import android.widget.Toast; public class MessengerActivity extends Activity implements ServiceConnection{ private Messenger messenger; private Boolean mBound = false; @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); bindService(new Intent(MessengerActivity.this,MessengerService.class),this, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); if(!mBound) { unbindService(this); } } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_messenger_activity); //send message to service findViewById(R.id.button_send_message_to_service).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { // a message is a reference to the Handler messenger.send(Message.obtain(null,MessengerService.MSG_SAY_HELLO,0,0)); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); // send message to the service to trigger a message to be sent back; findViewById(R.id.button_reveive_message_to_service).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { // a message is a reference to the Handler messenger.send(Message.obtain(null,MessengerService.MSG_FROM_SERVICE_TO_ACTIVITY,0,0)); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } // A handler used to receive message from service public class ActivityHandler extends Handler { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if(msg.what == MessengerService.MSG_FROM_SERVICE_TO_ACTIVITY) { Log.i(FFFF,Received Service's Message!); } } } @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { // a message is a reference to the Handler // use a messenger to wrap the binder,so can we send the message to service messenger = new Messenger(arg1); mBound = true; } @Override public void onServiceDisconnected(ComponentName arg0) { mBound = false; } }

?

Messenger和AIDL的比较:

?

Compared to AIDL

?

When you need to perform IPC, using a Messenger for your interface is simpler than implementing it with AIDL, because Messenger queues all calls to the service, whereas, a pure AIDL interface sends simultaneous requests to the service, which must then handle multi-threading.

For most applications, the service doesn't need to perform multi-threading, so using a Messengerallows the service to handle one call at a time. If it's important that your service be multi-threaded, then you should use AIDL to define your interface.


Binding to a Service


Application components (clients) can bind to a service by calling bindService(). The Android system then calls the service's onBind() method, which returns an IBinder for interacting with the service.

The binding is asynchronous. bindService() returns immediately and does not return the IBinder to the cl

首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ZOJ 3820 Building Fire Stations.. 下一篇POJ 3984 迷宫问题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·“我用Java 8”已成 (2025-12-26 11:19:54)
·下载 IntelliJ IDEA (2025-12-26 11:19:52)
·Java是什么?(通俗 (2025-12-26 11:19:49)
·雾里看花:真正意义 (2025-12-26 10:54:36)
·C++——模板(超详细 (2025-12-26 10:54:34)