Chinaunix首页 | 论坛 | 博客
  • 博客访问: 420155
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Android平台

2015-04-12 22:00:21

这一节笔记是关于Transact的IPC,Transact的使用背景和Messenger一样,是在同一个APP中Client与service的IPC
相对比,使用Transact比使用Messenger简单的多。了解BinderService基础,这个就so easy

MyService.java

  1. package com.vibexie.servicetransact;

  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.Binder;
  5. import android.os.IBinder;
  6. import android.os.Parcel;
  7. import android.os.RemoteException;

  8. public class MyService extends Service {
  9.     @Override
  10.     public void onCreate() {
  11.         super.onCreate();
  12.     }

  13.     public MyService() {
  14.     }

  15.     public class MyBinder extends Binder{
  16.         @Override
  17.         protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
  18.             reply.writeString(data.readString()+" and juan");
  19.             return super.onTransact(code, data, reply, flags);
  20.         }
  21.     }
  22.     @Override
  23.     public IBinder onBind(Intent intent) {
  24.         // TODO: Return the communication channel to the service.
  25.        return new MyBinder();
  26.     }
  27. }


MainActivity.java

  1. package com.vibexie.servicetransact;

  2. import android.content.ComponentName;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.os.IBinder;
  7. import android.os.Parcel;
  8. import android.os.RemoteException;
  9. import android.support.v7.app.ActionBarActivity;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.Toast;


  14. public class MainActivity extends ActionBarActivity {
  15.     /**
  16.      * 与service建立连接的Binder
  17.      */
  18.     MyService.MyBinder myBinder;
  19.     private Button button;
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.         button=(Button)this.findViewById(R.id.button);
  25.         Intent intent=new Intent(this,MyService.class);
  26.         bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);

  27.         button.setOnClickListener(new View.OnClickListener() {
  28.             @Override
  29.             public void onClick(View v) {
  30.                 /**
  31.                  * 向service发送的Parcel
  32.                  */
  33.                 Parcel parcel=Parcel.obtain();
  34.                 parcel.writeString("vibexie");
  35.                 /**
  36.                  * 用于接收service返回数据的parcel
  37.                  */
  38.                 Parcel reply=Parcel.obtain();

  39.                 /**
  40.                  * 与Service进行交互
  41.                  */
  42.                 try {
  43.                     myBinder.transact(IBinder.FIRST_CALL_TRANSACTION, parcel, reply, 0);
  44.                 } catch (RemoteException e) {
  45.                     e.printStackTrace();
  46.                 }
  47.                 
  48.                 /**
  49.                  * 获取返回数据
  50.                  */
  51.                 Toast.makeText(MainActivity.this,reply.readString(),Toast.LENGTH_SHORT).show();
  52.             }
  53.         });
  54.     }

  55.     ServiceConnection serviceConnection=new ServiceConnection() {
  56.         @Override
  57.         public void onServiceConnected(ComponentName name, IBinder service) {
  58.             myBinder = (MyService.MyBinder)service;
  59.         }

  60.         @Override
  61.         public void onServiceDisconnected(ComponentName name) {

  62.         }
  63.     };
  64. }
效果:
阅读(1409) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~