Android 兩個進程之間使用AIDL

來源:互聯網
上載者:User

我們假定兩個應用程式為A和B。

我的意圖是在B應用工程中建立Aidl,然後在這裡實現 aidl裡面的方法,然後在A應用工程中去遠程調用:

好了:我先去建立 B應用程式:package com.king.android.service;

interface IService{

    //設定地址
    void setAddress(String address);
    
    //設定國家
    void setCountry(String country);
    
    String disPlay();

}

在Aidl程式中不要任何存取修飾詞,也不要 static,final 來修飾 ,另外其它的資料類型會有所不同,後期再說:

好了,建立完這個aidl檔案,然後就需要我去去實現這個aidl介面的方法:

 package com.king.android.service;

import android.os.RemoteException;

/**

 * 描述:實現IService介面
 * 作者:Andy.Liu
 * 時間: 2012-7-30  上午08:41:02
 **/
public class IServiceImpl extends IService.Stub{
    private String mAddress = null;
    private String mCountry = null;

    @Override
    public void setAddress(String address) throws RemoteException {
        this.mAddress = address;
    }

    @Override
    public void setCountry(String country) throws RemoteException {
        this.mCountry = country;
    }

    @Override
    public String disPlay() throws RemoteException {
        
        return  "address:"+mAddress+"--------------mCountry:"+mCountry;
    }

}

 上面是實現這個aidl介面的實作類別。

 

把自己的aidl暴露出來,供別的應用程式使用:

package com.king.android.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.king.android.service.IService.Stub;

/**

 * 描述:將實現的aidl介面暴露給用戶端
 * 作者:Andy.Liu
 * 時間: 2012-8-2  上午07:01:49
 **/
public class MyRemoteSerivce extends Service {
    public static final String MY_SERIVCE = "com.king.android.MY_REMOTE_SERIVCE";

    private Stub iService = new IServiceImpl();
    @Override
    public IBinder onBind(Intent intent) {
        return iService;
    }

另外需要在Androidmenifest.xml檔案中去註冊這個服務。以及過濾。。。 

這樣在B程式程式中的準備工作做完了。。。

 

 A工程中準備工作:

直接把B應用程式中aidl檔案所在的包以及,這個Aidl檔案copy到 A應用程式中。並儲存。。。

好了,這樣我們就可以去在A應用程式中調用B應用程式提供的介面方法。。。

package com.king.android.controls;

import com.king.android.R;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    public static final String MY_SERIVCE = "com.king.android.MY_REMOTE_SERIVCE";//服務指定的動作
   
    private IService iService;
    private Button btn;
    MyReceiver2 receiver = new MyReceiver2();

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        //執行個體化MYReceiver2
        registerReceiver(receiver, filter);
    }
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.btn_voice);
        btn.setVisibility(View.VISIBLE);
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(MY_SERVICE);
                bindService(intent, conn, Service.BIND_AUTO_CREATE);
            }
        });
    }
    
    
    
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        synchronized public void onServiceConnected(ComponentName name, IBinder service) {
            iPerson = IPerson.Stub.asInterface(service);
            if(null!=iPerson){
                //RPC調用方法
                try {
                    iPerson.setAddress("shenzhen.guangdong");
                    iPerson.setCountry("china");
                    String msg = iPerson.display();
                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
                } catch (RemoteException e) {
                    
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

 在onServiceConnedted中傳入需要的參數。

然後去綁定這個服務即可。 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.