android aidl 進程間通訊

傳遞數(shù)據(jù)

自定義類型 要實現(xiàn)parceable 接口
并定義同名的aidl文件 文件內(nèi)容parcealbe 類名


實現(xiàn)接口回調(diào):

要實現(xiàn)接口 接口用aidl定義這樣才能執(zhí)行,在不同進程間的對象的hashcode是不樣的,因此正常是無法回調(diào)的
接口也用aidl MyLatLngListenerAidl.aidl

package com.p2.pa.amap.location;  
interface MyLatLngListenerAidl  
{  
    void receiverLatLng(double lat,double lon);  
}  

實現(xiàn)類



public class MyLatLngListener extends MyLatLngListenerAidl.Stub {  
    ActivityMain context;  
    private AMap map;  
  
    public MyLatLngListener(AMap map, ActivityMain activityMain) {  
        super();  
        this.map = map;  
        this.context = activityMain;  
    }  
    @Override  
    public void receiverLatLng(double lat, double lon) throws RemoteException {  
        System.out.println("MyLatLngListener receiverLatLng lat:" + lat  
                + ",lon:" + lon);  
        locationOk(new LatLng(lat, lon));  
    }  

服務(wù)調(diào)用,綁定連接成功后添加偵聽


class MyLocatonConnection implements ServiceConnection {  
  
  
    @Override  
    public void onServiceConnected(ComponentName arg0, IBinder arg1) {  
        printLog("onServiceConnected");  
        aidl=LocationServiceAidl.Stub.asInterface(arg1);  
        isBind = true;  
        try {  
            MyLatLngListener listener = new MyLatLngListener(aMap, ActivityMain.this);  
            printLog("MyLatLngListener:"+listener.hashCode());  
            aidl.addListener(listener);  
        } catch (RemoteException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
  
    }  

參考 http://item.congci.com/item/aidl-hui-diao-hanshu-tongbu-tongxun

//android service一直再運行,通過bindService拿到service的代理,并將自己到回調(diào)對象注冊過去,就能實現(xiàn)調(diào)用service中的方法,和在service中調(diào)用本地activity到方//法。做到了進程間通信。

ImyserviceManager.aidl

package com.test;  
import com.test.Ilisten;  
interface ImyserviceManager  
{  
 int add(int a,int b);  
 String show();  
 void register(Ilisten listen);  
}  

RemoteService.java

package com.test;  
import android.app.Service;  
import android.content.Intent;  
import android.os.IBinder;  
import android.os.RemoteException;  
import android.util.Log;  
public class RemoteService extends Service  
{  
  Ilisten myListener = null;  
  public class ServiceImpl extends ImyserviceManager.Stub  
  {  
      public int add(int a,int b)throws RemoteException  
      {  
          if(myListener != null)  
              myListener.change("this is call back!");  
          return (a+b);  
      }  
        
      public String show()throws RemoteException  
      {  
          return "hello world!";  
      }  
      public void register(Ilisten listen) throws RemoteException  
      {  
          // TODO Auto-generated method stub  
          myListener = listen;  
      }  
  }  
    
  @Override  
  public IBinder onBind(Intent intent)  
  {  
      // TODO Auto-generated method stub  
      return new ServiceImpl();  
  }  
  @Override  
  public int onStartCommand(Intent intent, int flags, int startId) {  
      // TODO Auto-generated method stub  
      Log.i("test","I am running .......................");  
      return super.onStartCommand(intent, flags, startId);  
        
  }  
    
    
}  

Ilisten.aidl

package com.test;  
interface Ilisten  
{  
  void change(String a);  
}  

TestAidl.java

package com.test;  
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;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
public class TestAidl extends Activity  
{  
    String str = null;  
    private ImyserviceManager myManager;  
    Button myButton;  
    private TextView textView;  
    private Button button1;  
    private Button button2;  
      
    private ServiceConnection serviceConnection =new ServiceConnection()  
    {  
        public void onServiceConnected(ComponentName name, IBinder service)  
        {  
            // TODO Auto-generated method stub+  
              
            myManager=ImyserviceManager.Stub.asInterface(service);  
            try {  
                Log.i("test-------",myManager.show());  
                TextView textView=(TextView)findViewById(R.id.text);  
                textView.setText(str);  
                myManager.register(new myListener());  
                  
            } catch (RemoteException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
        public void onServiceDisconnected(ComponentName name)  
        {  
            // TODO Auto-generated method stub  
              
        }  
          
    };  
      
    public class myListener extends Ilisten.Stub  
    {  
        public void change(String a) throws RemoteException  
        {  
            // TODO Auto-generated method stub  
              
            button1.setText(a);  
              
        }  
          
    }  
      
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        bindService(new Intent(TestAidl.this, RemoteService.class), serviceConnection, Context.BIND_AUTO_CREATE);  
    
         textView=(TextView)findViewById(R.id.text);  
          
         button1 = (Button) findViewById(R.id.b1);  
          
         button1.setOnClickListener(new View.OnClickListener() {  
              
            public void onClick(View v)  
            {  
                try {  
                    button1.setText(myManager.show());  
                    //myManager.add(1, 2);  
                } catch (RemoteException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
        });  
          
         button2= (Button)findViewById(R.id.b2);  
         button2.setOnClickListener(new View.OnClickListener() {  
              
            public void onClick(View v)  
            {  
                try {  
                    myManager.add(2, 3);  
                } catch (RemoteException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
        });  
      
    }  
}  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容