资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
1Android 开发Activity 与 service 使用 Messenger 通信Messenger 对象可以发送 Message,送出的消息放在消息池中,由主线程依次取出,交给和 Message 关联的 Handle 处理,而 Messenger 可以在创建时和 Handle 对象关联,所以由哪个 Messenger 发出的 Message,就可以由和这个 Messenger 关联的 Handle 处理。所以为了实现 Activity 和 Service 的通信,我们可以在 Activity 和 Service 中分别定义一个Handle 类,用来处理和这个 Handle 相关联的 Messenger 发出的 Message。并且用这个类对象去创建 Messenger 对象。具体例子如下:Activity 的代码public class SimpleMathServiceDemo extends Activity /private MathService mathService;private Messenger sMessenger=null;private boolean isBound = false;TextView labelView;private final Messenger aMessenger=new Messenger(new ActivityHandle();class ActivityHandle extends HandlerOverridepublic void handleMessage(Message msg) / TODO Auto-generated method stubBundle bundle=new Bundle();bundle=msg.getData();labelView.setText(bundle.getCharSequence(fromservice);super.handleMessage(msg);Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);labelView = (TextView)findViewById(R.id.label);Button bindButton = (Button)findViewById(R.id.bind);Button unbindButton = (Button)findViewById(R.id.unbind);Button computButton = (Button)findViewById(R.id.compute);bindButton.setOnClickListener(new View.OnClickListener()Override2public void onClick(View v) if(!isBound)final Intent serviceIntent = new Intent(SimpleMathServiceDemo.this,MathService.class);bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE);isBound = true; );unbindButton.setOnClickListener(new View.OnClickListener()Overridepublic void onClick(View v) if(isBound)isBound = false;unbindService(mConnection); );computButton.setOnClickListener(new View.OnClickListener()Overridepublic void onClick(View v) if(sMessenger!=null)try Bundle bundle=new Bundle();bundle.putString(fromActivity, 从Activity发向Service);Message msg=Message.obtain();msg.setData(bundle);msg.replyTo=aMessenger;sMessenger.send(msg); catch (RemoteException e) / TODO Auto-generated catch blocke.printStackTrace();elselabelView.setText(没有启动服务! ); 3);private ServiceConnection mConnection = new ServiceConnection() Overridepublic void onServiceConnected(ComponentName name, IBinder service) Log.i(Service, onServiceConnected);sMessenger=new Messenger(service);Overridepublic void onServiceDisconnected(ComponentName name) sMessenger=null;Log.i(Service, onServiceDisconnected);Overridepublic boolean bindService(Intent service, ServiceConnection conn, int flags) / TODO Auto-generated method stubLog.i(Service, bindService);return super.bindService(service, conn, flags);Service 中的代码:public class MathService extends Serviceprivate final Messenger sMessenger = new Messenger(new ServiceHandle();public Messenger aMessenger=null;public class ServiceHandle extends HandlerOverridepublic void handleMessage(Message msg) / TODO Auto-generated method stubBundle bundle=new Bundle();bundle=msg.getData();aMessenger=msg.replyTo;4Toast.makeText(MathService.this, bundle.get(fromActivity).toString(), Toast.LENGTH_LONG).show();Bundle bundle1=new Bundle();bundle1.putString(fromservice, 从service到activity);Message msg1=Message.obtain();msg1.setData(bundle1);try aMessenger.send(msg1); catch (RemoteException e) / TODO Auto-generated catch blocke.printStackTrace();super.handleMessage(msg);public class LocalBinder extends BinderMathService getService() return MathService.this;Overridepublic IBinder onBind(Intent intent) Toast.makeText(this, 本地绑定:MathService, Toast.LENGTH_SHORT).show();Log.i(Service, onBind);return sMessenger.getBinder();Overridepublic boolean onUnbind(Intent intent)Toast.makeText(this, 取消本地绑定:MathService, Toast.LENGTH_SHORT).show(); Log.i(Service, onUnbind);return false;5Overridepublic void onCreate() / TODO Auto-generated method stubsuper.onCreate();Log.i(Service, onCreat);Overridepublic void onDestroy() / TODO Auto-generated method stubsuper.onDestroy();Log.i(Service, onDestroy);Overridepublic void onRebind(Intent intent) / TODO Auto-generated method stubsuper.onRebind(intent);Log.i(Service, onRebind);Overridepublic void onStart(Intent intent, int startId) / TODO Auto-generated method stubsuper.onStart(intent, startId);Log.i(Service, onStart);例中,Activity 中定义了 ActivityHandle 类,并重载了 public void handleMessage(Message msg)方法用来处理和该 Handle 关联的消息。并通过 private final Messenger aMessenger=new Messenger(new ActivityHandle();创建了 Activity 的数据成员 aMessenger,如果 Service 能获取aMessenger 对象,那么就可以利用它给 Activity 传递消息了。同样,Service 中也定义了 ServiceHandle 类,并通过 private final Messenger sMessenger=new Messenger(new ServiceHandle();创建了 Service 的数据成员sMessenger,如果 Activity 能获取 sMessenger 对象,那么就可以利用它给 Service 传递消息了。那么 Activity 如何获取 Service 的 Messenger 呢,它是通过 Activity 调用 Service过程中传递过来的参数 IBinder 对象,通过构造方法创建的 sMessenger 对象,其中IBinder 对象和 Service 中的 sMessenger 关联。而 Activity 在使用 sMessenger 发送Message 对象时,先设置 Message 对象的 Replyto 属性为 aMessenger,那么 Service6在收到消息进
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号