ITPub博客

首页 > 应用开发 > IT综合 > Andriod教程 AIDL的使用

Andriod教程 AIDL的使用

原创 IT综合 作者:husky222_ 时间:2014-12-18 11:01:22 0 删除 编辑
 

Andriod开发中了解AIDL的使用是很重要的,这是andriod开发中必备的一个技能。全称为“Android Interface Definition Language”,它是一种IDL 语言,用来生成可以在Android设备上两个进程之间进行进程间通信的代码。若在一个进程中(比如说Activity),想调用另外一个进程中(例如Service)对象的操作,便能使用AIDL生成可序列化的参数。下面简单介绍一下AIDL使用的方法:

1.新建IRemoteService.aidl

[java] view plaincopyprint?

1.  package com.tang.remoteservicedemo;  

2.  interface IRemoteService {  

3.   String getInfo();  

4.  }  

package com.tang.remoteservicedemo;

interface IRemoteService {

 String getInfo();

}

从内容中也可以看出,这东西类似一个接口。既然定义了这么一个玩意,那么我们就要去实现它。

2.新建IService“实现”IRemoteService“接口”

[java] view plaincopyprint?

1.  package com.tang.remoteservicedemo;  

2.    

3.  import java.util.Date;  

4.    

5.  import android.app.Service;  

6.  import android.content.Intent;  

7.  import android.os.IBinder;  

8.  import android.os.RemoteException;  

9.    

10. public class IService extends Service  

11. {  

12.     private IBinder iBinder = new IRemoteService.Stub() {  

13.         @Override  

14.         public String getInfo() throws RemoteException {  

15.             // TODO Auto-generated method stub   

16.             return new Date(System.currentTimeMillis()).toLocaleString()+"  来自远程服务的信息!";  

17.         }  

18.     };  

19.     @Override  

20.     public IBinder onBind(Intent intent) {  

21.         // TODO Auto-generated method stub   

22.         return iBinder;  

23.     }  

24. }  

package com.tang.remoteservicedemo;

import java.util.Date;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.os.RemoteException;

public class IService extends Service

{

  private IBinder iBinder = new IRemoteService.Stub() {

    @Override

    public String getInfo() throws RemoteException {

     // TODO Auto-generated method stub

     return new Date(System.currentTimeMillis()).toLocaleString()+"  来自远程服务的信息!";

    }

  };

  @Override

  public IBinder onBind(Intent intent) {

    // TODO Auto-generated method stub

    return iBinder;

  }

}

基于Binder的不同进程间通信,Client与Service在不同的进程中,对用户程序而言当调用Service返回的IBinder接口后,访问Service中的方法就如同调用自己的函数一样。

3.配置IService供其他进程调用

[java] view plaincopyprint?

1.  "com.tang.remoteservicedemo.IService"  

2.      android:process=":remote">  

3.        

4.          "com.tang.remoteservicedemo.IService"/>  

5.        

6.    

       

            android:process=":remote">

           

               

           

       


配置一个所属进程名和一个action。

通过前面三个步骤,这个含有getInfo()方法的service就可以给别人调用了,下面在客户端调用它

4.新建一个ClientDemo工程将含有IRemoteService.aidl的那个包全部拷贝到src下,只留下aidl文件,其他全删除。

               

5.新建MainActivity,其他就是绑定service的操作了

[java] view plaincopyprint?

1.  package com.tang.clientdemo;  

2.    

3.  import java.util.Timer;  

4.  import java.util.TimerTask;  

5.    

6.  import com.tang.remoteservicedemo.IRemoteService;  

7.    

8.  import android.app.Activity;  

9.  import android.content.ComponentName;  

10. import android.content.Context;  

11. import android.content.Intent;  

12. import android.content.ServiceConnection;  

13. import android.os.Bundle;  

14. import android.os.IBinder;  

15. import android.os.RemoteException;  

16. import android.util.Log;  

17.   

18.   

19. public class MainActivity extends Activity {  

20.   

21.     private IRemoteService iService = null;  

22.     private boolean isBinded =false;  

23.       

24.     ServiceConnection conn = new ServiceConnection() {  

25.           

26.         @Override  

27.         public void onServiceDisconnected(ComponentName name) {  

28.             // TODO Auto-generated method stub   

29.             isBinded = false;  

30.             iService = null;  

31.         }  

32.           

33.         @Override  

34.         public void onServiceConnected(ComponentName name, IBinder service) {  

35.             // TODO Auto-generated method stub   

36.             iService = IRemoteService.Stub.asInterface(service);  

37.             isBinded = true;  

38.         }  

39.     };  

40.       

41.     public void doBind()   

42.     {  

43.         Intent intent = new Intent("com.tang.remoteservicedemo.IService");  

44.         bindService(intent, conn, Context.BIND_AUTO_CREATE);  

45.     }  

46.       

47.     public void doUnbind()   

48.     {  

49.         if (isBinded)   

50.         {  

51.           unbindService(conn);  

52.           iService = null;  

53.           isBinded = false;  

54.         }  

55.     }  

56.     @Override  

57.     protected void onCreate(Bundle savedInstanceState) {  

58.         super.onCreate(savedInstanceState);  

59.         setContentView(R.layout.activity_main);  

60.         new Thread(new Runnable() {       

61.             @Override  

62.             public void run() {  

63.                 // TODO Auto-generated method stub   

64.                 doBind();  

65.             }  

66.         }).start();  

67.         Timer timer = new Timer();  

68.         timer.schedule(task, 0, 2000);  

69.           

70.     }  

71.     TimerTask task = new TimerTask() {  

72.         @Override  

73.         public void run() {  

74.             // TODO Auto-generated method stub   

75.             if(iService!=null)  

76.             {  

77.                 try {  

78.                     Log.i("AAA", iService.getInfo());  

79.                 } catch (RemoteException e) {  

80.                     // TODO Auto-generated catch block   

81.                     e.printStackTrace();  

82.                 }  

83.             }  

84.             else  

85.             {  

86.                 Log.i("AAA", "iService!=null");  

87.             }  

88.               

89.         }  

90.     };  

91.   

92.     @Override  

93.     protected void onDestroy() {  

94.         // TODO Auto-generated method stub   

95.         doUnbind();  

96.         task.cancel();  

97.         super.onDestroy();  

98.     }  

99. }  

package com.tang.clientdemo;

import java.util.Timer;

import java.util.TimerTask;

import com.tang.remoteservicedemo.IRemoteService;

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.IBinder;

import android.os.RemoteException;

import android.util.Log;

public class MainActivity extends Activity {

  private IRemoteService iService = null;

  private boolean isBinded =false;

 

  ServiceConnection conn = new ServiceConnection() {

  

   @Override

   public void onServiceDisconnected(ComponentName name) {

     // TODO Auto-generated method stub

     isBinded = false;

     iService = null;

   }

  

   @Override

   public void onServiceConnected(ComponentName name, IBinder service) {

     // TODO Auto-generated method stub

     iService = IRemoteService.Stub.asInterface(service);

     isBinded = true;

   }

  };

 

  public void doBind()

  {

      Intent intent = new Intent("com.tang.remoteservicedemo.IService");

      bindService(intent, conn, Context.BIND_AUTO_CREATE);

  }

 

  public void doUnbind()

  {

      if (isBinded)

      {

        unbindService(conn);

        iService = null;

        isBinded = false;

      }

  }

  @Override

  protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

   setContentView(R.layout.activity_main);

   new Thread(new Runnable() {  

     @Override

     public void run() {

      // TODO Auto-generated method stub

      doBind();

     }

   }).start();

   Timer timer = new Timer();

   timer.schedule(task, 0, 2000);

  

  }

  TimerTask task = new TimerTask() {

   @Override

   public void run() {

     // TODO Auto-generated method stub

     if(iService!=null)

     {

      try {

        Log.i("AAA", iService.getInfo());

      } catch (RemoteException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

      }

     }

     else

     {

      Log.i("AAA", "iService!=null");

     }

    

   }

  };

  @Override

  protected void onDestroy() {

   // TODO Auto-generated method stub

   doUnbind();

   task.cancel();

   super.onDestroy();

  }

}


执行之后的有一个为空的Log呢,到底是为什么呢?这是因为绑定也是要时间的哈...

以上就是简单的AIDL的介绍


本文来自麦子学院:http://www.maiziedu.com/

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30037762/viewspace-1371906/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-11-25

  • 博文量
    5
  • 访问量
    20391