设为首页 加入收藏

TOP

Bound Service的三种方式(Binder、 Messenger、 AIDL)(二)
2015-07-20 17:28:22 来源: 作者: 【 】 浏览:27
Tags:Bound Service 方式 Binder Messenger AIDL
ler that responds to different types of Message objects. This Handler is the basis for a Messenger that can then share an IBinder with the client, allowing the client to send commands to the service using Message objects. Additionally, the client can define a Messenger of its own so the service can send messages back.

This is the simplest way to perform interprocess communication (IPC), because the Messenger queues all requests into a single thread so that you don't have to design your service to be thread-safe.

?

If you need your service to communicate with remote processes, then you can use a Messenger to provide the interface for your service. This technique allows you to perform interprocess communication (IPC) without the need to use AIDL.

Here's a summary of how to use a Messenger:

  • The service implements a Handler that receives a callback for each call from a client.
  • The Handler is used to create a Messenger object (which is a reference to the Handler).
  • The Messenger creates an IBinder that the service returns to clients from onBind().
  • Clients use the IBinder to instantiate the Messenger (that references the service's Handler), which the client uses to sendMessage objects to the service.
  • The service receives each Message in its Handler—specifically, in the handleMessage() method.

    In this way, there are no methods for the client to call on the service. Instead, the client delivers messages (Message objects) that the service receives in its Handler.

    下面的程序试验了两个功能:Activity发送消息给Service,Service产生一个Toast;Activity发送消息给Service,Service再返回一个消息给Activity

    Service 代码如下:

    ?

    ?

    package com.example.boundservice;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Handler;
    import android.os.IBinder;
    import android.os.Message;
    import android.os.Messenger;
    import android.os.RemoteException;
    import android.widget.Toast;
    
    public class MessengerService extends Service {
    	static final int MSG_SAY_HELLO = 0;
    	static final int MSG_FROM_SERVICE_TO_ACTIVITY = 1;
    	
    	// step 1
    	class IncomingHandler extends Handler {
    		@Override
    		public void handleMessage(Message msg) {
    			// TODO Auto-generated method stub
    			switch (msg.what) {
    			case MSG_SAY_HELLO:
    				Toast.makeText(getApplicationContext(), Service:Hello!, Toast.LENGTH_SHORT).show();
    				break;
    			// receive activity's message and send it back;
    			case MSG_FROM_SERVICE_TO_ACTIVITY:
    				Messenger activityMessenger = new Messenger(new MessengerActivity().new ActivityHandler());
    				try {
    					activityMessenger.send(Message.obtain(null,MessengerService.MSG_FROM_SERVICE_TO_ACTIVITY,0,0));
    				} catch (RemoteException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				break;
    			default:
    				super.handleMessage(msg);
    			}
    		}
    	}
    	// step 2
    	Messenger messenger = new Messenger(new IncomingHandler());
    	// step 3
    	@Override
    	public IBinder onBind(Intent arg0) {
    		// TODO Auto-generated method stub
    		return messenger.getBinder();
    	}
    	
    }
    


    Activity代码如下:

    ?

    ?

    package com.example.boundservice;
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.IBinder;
    import android.os.Message;
    import android.os.Messenger;
    im
首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ZOJ 3820 Building Fire Stations.. 下一篇POJ 3984 迷宫问题

评论

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

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)