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

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Android平台

2015-04-12 16:27:32

这篇笔记是关于AIDL的
对于AIDL,在官方文档中不好理解。
这里写出建立AIDL工程的过程。

/*这里需要提一下,很多时候,我们可以通过android序列化将对象在进程间传递,这是一种不错的选择,实现很简单,只要让一个类实现pacelable接口就OK,其传递和普通类型的传递一样*/

首先写服务端
1.建立AIDL文件。在androids studio中建AIDL文件要注意的地方-->http://blog.chinaunix.net/uid-29532371-id-4905575.html
    

RemoteServiceAidl.java

  1. // RomoteServiceAidl.aidl
  2. package com.vibexie.myaidl;

  3. // Declare any non-default types here with import statements

  4. interface RemoteServiceAidl {
  5.     /**
  6.      *在Android Studio中写AIDL文件,必须没次修改AIDL文件后,要重新Make Project,make还是不行的话,重新打开工程即可
  7.      *对于java基本类型和String类型,默认是in,可以注明in,out,还是inout,但List,Map等类型必须指定,否者报错
  8.      *这里举getList这个方法中,Client调用getList(),传入list,list2,其中list已经赋值,在远程Service中,将list的值赋给list2,
  9.      *并且在Client的getList中直接更新了list2,这就是in和out的作用
  10.      */
  11.     String getData(String name);
  12.     void getList(in List<String> list,out List<String> list2);
  13. }
2.建立Service,在Service中实现AIDL接口

RemoteService.java

  1. package com.vibexie.myaidl;

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

  8. import java.util.List;

  9. public class RemoteService extends Service {

  10.     @Override
  11.     public void onCreate() {
  12.         super.onCreate();
  13.         Toast.makeText(getApplicationContext(),"启动Service",Toast.LENGTH_SHORT).show();
  14.     }

  15.     /**
  16.      * new一个binder实现AIDL接口,并将binder通过onBind()返回给service
  17.      */
  18.     Binder binder=new RemoteServiceAidl.Stub() {
  19.         @Override
  20.         public String getData(String name) throws RemoteException {
  21.             return name+"+1";
  22.         }

  23.         @Override
  24.         public void getList(List<String> list,List<String> list2) throws RemoteException {
  25.             /**
  26.              * 这里将list的第一个String赋给list2的第一个String,这就是service的服务功能
  27.              */
  28.             list2.add(list.get(0));
  29.         }
  30.     };

  31.     @Override
  32.     public IBinder onBind(Intent intent) {
  33.         // TODO: Return the communication channel to the service.
  34.         return binder;
  35.     }
  36. }

3.在ActivityMainfest.xml注册服务,并且过滤AIDL

点击(此处)折叠或打开

  1. <service
  2.             android:name=".RemoteService"
  3.             android:enabled="true"
  4.             android:exported="true" >
  5.             <intent-filter>
  6.                 <action android:name="com.vibexie.myaidl.RemoteServiceAidl"></action>
  7.             </intent-filter>
  8. </service>



4.在Activity中启动Service

ActivityMain.java

  1. package com.vibexie.myaidl;

  2. import android.content.Intent;
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.view.View;
  8. import android.widget.Button;


  9. public class MainActivity extends ActionBarActivity {
  10.     private Button button;
  11.     private Intent intent;
  12.     @Override
  13.     protected void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.activity_main);
  16.         button=(Button)this.findViewById(R.id.button);
  17.         button.setOnClickListener(new View.OnClickListener() {
  18.             @Override
  19.             public void onClick(View v) {
  20.                 intent=new Intent(MainActivity.this,RemoteService.class);
  21.                 startService(intent);
  22.             }
  23.         });
  24.     }

  25.     /**
  26.      * 记得关闭service,否者service不会stop
  27.      */
  28.     @Override
  29.     protected void onDestroy() {
  30.         super.onDestroy();
  31.         stopService(intent);
  32.     }
  33. }
这样服务端Service就写好了。
接下来就是客户端Client了
1.定义与服务端一模一样的AIDL文件,注意包名必须和服务端AIDL所在的包名一致,否则报错

2.建立Activity,调用远程服务

MainActivity.java

  1. package com.vibexie.myaidl;

  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.RemoteException;
  8. import android.support.v7.app.ActionBarActivity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.Toast;

  13. import java.util.ArrayList;
  14. import java.util.List;


  15. public class MainActivity extends ActionBarActivity {
  16.     private Button button;
  17.     private Button button2;
  18.     private RemoteServiceAidl remoteServiceAidl;
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.         button=(Button)this.findViewById(R.id.button);
  24.         button2=(Button)this.findViewById(R.id.button2);

  25.         button.setOnClickListener(new View.OnClickListener() {
  26.             @Override
  27.             public void onClick(View v) {
  28.                 /**
  29.                  * 绑定远程Service
  30.                  */
  31.                 Intent intent=new Intent(RemoteServiceAidl.class.getName());
  32.                 bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
  33.                 Toast.makeText(MainActivity.this,"绑定service",Toast.LENGTH_SHORT).show();
  34.             }
  35.         });

  36.         button2.setOnClickListener(new View.OnClickListener() {
  37.             @Override
  38.             public void onClick(View v) {
  39.                 try {
  40.                     //Toast.makeText(MainActivity.this,""+remoteServiceAidl.getData("vibexie"),Toast.LENGTH_SHORT).show();
  41.                     List<String> list=new ArrayList<String>();
  42.                     List<String> list2=new ArrayList<String>();
  43.                     list.add("vibexie");
  44.                     /**
  45.                      * 调用远程service接口方法,请求服务
  46.                      */
  47.                     remoteServiceAidl.getList(list,list2);
  48.                     Toast.makeText(MainActivity.this,""+"list2被赋值"+list2.get(0),Toast.LENGTH_SHORT).show();
  49.                 } catch (RemoteException e) {
  50.                     e.printStackTrace();
  51.                 }
  52.             }
  53.         });
  54.     }

  55.     private ServiceConnection serviceConnection=new ServiceConnection() {
  56.         @Override
  57.         public void onServiceConnected(ComponentName name, IBinder service) {
  58.             remoteServiceAidl=RemoteServiceAidl.Stub.asInterface(service);
  59.         }
  60.         
  61.         @Override
  62.         public void onServiceDisconnected(ComponentName name) {
  63.         }
  64.     };
  65. }
两步完成了,就OK了。效果如下:
服务端:

客户端:
阅读(1282) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~