进程间通信---AIDL的使用实例


aidl文件

  1. package com.tang.remoteservicedemo;  
  2. interface IRemoteService {  
  3.  String getInfo();  
  4. }  


实现
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;
}
}



应用实现
  1. <service android:name="com.tang.remoteservicedemo.IService"  
  2.     android:process=":remote">  
  3.     <intent-filter>  
  4.         <action android:name="com.tang.remoteservicedemo.IService"/>  
  5.     </intent-filter>  
  6. </service>  



实现:
  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, 02000);  
  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. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值