Android Serivce 進階篇AIDL講解

來源:互聯網
上載者:User
 說明:要使用AIDL 1.先建立一個aidl檔案,如下面的IPerson.aidl

2.建立完畢後,重新整理一下,可以在R.gen下面看到代理實現的Iperson.java檔案

3.實現aidl介面,如IpersonImpl.java就是實現IPerson.aidl中的介面

4.使用Service將介面暴露給用戶端調用。如下面的MyRemoteService.java

5.用戶端調用這個介面,即可。

後記:後面會在兩個應用程式之間進行資料調用。 

 

 

package com.king.android.controls;

//IPerson介面
interface IPerson{

    //設定年齡
    void setAge(int age);
    
    //設定姓名
    void setName(String name);
    
    //資訊展示
    String display();
}

package com.king.android.controls;

import android.os.RemoteException;

/**

 * 描述:實現Iperson.aidl介面
 * 作者:Andy.Liu
 * 時間: 2012-7-18  下午10:44:27
 **/
public class IPersonImpl extends IPerson.Stub {
    private int age;
    private String name;

    @Override
    public String display() throws RemoteException {
        return "name:"+name+";age="+age;
    }

    @Override
    public void setAge(int age) throws RemoteException {
        this.age = age;
    }

    @Override
    public void setName(String name) throws RemoteException {
        this.name = name;
    }

}

package com.king.android.controls;

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

import com.king.android.controls.IPerson.Stub;

/**

 * 描述:使用service將介面暴露給用戶端
 * 作者:Andy.Liu
 * 時間: 2012-7-18  下午10:47:47
 **/
public class MyRemoteService extends Service {

    //聲明Iperson介面
    private Stub iPerson = new IPersonImpl();
    @Override
    public IBinder onBind(Intent intent) {

        return iPerson;
    }

}

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

/**

 * 描述:測試Aidl
 * 作者:Andy.Liu
 * 時間: 2012-7-18  下午10:54:00
 **/
public class MainActivity extends Activity {
    private static final String MY_SERVICE = "com.king.android.controls.MY_REMOTE_SERVICE";//服務指定的動作
    //聲明IPerson介面
    private IPerson iPerson;
    private Button btn;
    //執行個體化Serviceconnection 
    
    @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.setName("king.android.iphone");
                    iPerson.setAge(30);
                    String msg = iPerson.display();
                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
                } catch (RemoteException e) {
                    
                    e.printStackTrace();
                }
            }
        }

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.