天天看點

android AIDL的使用

android AIDL的使用
android AIDL的使用

  上圖為所建工程結構 因為android AIDL建立時需要包名相同 ,是以建議copy,注意運作時先運作一次service,不要犯低級錯誤哈 

  1:服務端和用戶端都有的相同的aidl檔案,自動生成檔案就不貼了

package com.pengxiaolog.aidl;

interface ServiceAidl{

   void setName(String name);

   String getName();

}

  2:服務端Service 類

package com.pengxiaolog.aidl;

import com.pengxiaolog.aidl.ServiceAidl;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.os.RemoteException;

import android.util.Log;

public class MyService extends Service{

private String Myname;

class MyBind extends ServiceAidl.Stub{

@Override

public void setName(String name) throws RemoteException {

// TODO Auto-generated method stub

Myname=name;

Log.e("================>>you [set] my name is", Myname);

}

@Override

public String getName() throws RemoteException {

// TODO Auto-generated method stub

Log.e("================>>you [get] my name is", Myname);

return Myname;

}

}

@Override

public void onCreate() {

// TODO Auto-generated method stub

super.onCreate();

}

@Override

public IBinder onBind(Intent arg0) {

// TODO Auto-generated method stub

return new MyBind();

}

@Override

public boolean onUnbind(Intent intent) {

// TODO Auto-generated method stub

return super.onUnbind(intent);

}

@Override

public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

}

下面貼出服務端以及用戶端的測試Activity 注意兩者的調用差別

 Service工程activity類:

       package com.pengxiaolog.aidl;

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

import android.view.View.OnClickListener;

import android.widget.Button;

import com.example.androidaidlservce.R;

public class MainActivitty extends Activity implements OnClickListener{

private ServiceAidl myService;

private Button btn[];

private int id[]=new int[]{R.id.button1,R.id.button2,R.id.button3,R.id.button4};

ServiceConnection serviceConnection=new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName arg0) {

// TODO Auto-generated method stub

myService=null; 

}

@Override

public void onServiceConnected(ComponentName arg0, IBinder arg1) {

// TODO Auto-generated method stub

myService=ServiceAidl.Stub.asInterface(arg1);

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

viewInit();

}

private void viewInit(){

btn=new Button[4];

for (int i = 0; i < btn.length; i++) {

btn[i]=(Button) this.findViewById(id[i]);

btn[i].setOnClickListener(this);

}

}

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

switch(arg0.getId()){

case R.id.button1:

bindService(new Intent("COM.PENGXIAOLONG.STYLE"), serviceConnection, Context.BIND_AUTO_CREATE); 

break;

case R.id.button2:

unbindService(serviceConnection);

break;

case R.id.button3:

try {

if(myService!=null)

myService.setName("NAME");

} catch (RemoteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

break;

case R.id.button4:

try {

if(myService!=null)

myService.getName();

} catch (RemoteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

break;

}

}

}

}

 client 端activity類:

package com.pengxiaolog.aidl;

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

import android.view.View.OnClickListener;

import android.widget.Button;

import com.example.androidaidlclient.R;

public class Mainactivity extends Activity implements OnClickListener{

private ServiceAidl myService;

private Button btn[];

private int id[]=new int[]{R.id.button1,R.id.button2,R.id.button3,R.id.button4};

ServiceConnection serviceConnection=new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName arg0) {

// TODO Auto-generated method stub

myService=null; 

}

@Override

public void onServiceConnected(ComponentName arg0, IBinder arg1) {

// TODO Auto-generated method stub

myService=ServiceAidl.Stub.asInterface(arg1);

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

viewInit();

}

private void viewInit(){

btn=new Button[4];

for (int i = 0; i < btn.length; i++) {

btn[i]=(Button) this.findViewById(id[i]);

btn[i].setOnClickListener(this);

}

}

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

switch(arg0.getId()){

case R.id.button1:

bindService(new Intent("COM.PENGXIAOLONG.STYLE"), serviceConnection, Context.BIND_AUTO_CREATE); 

break;

case R.id.button2:

unbindService(serviceConnection);

break;

case R.id.button3:

try {

if(myService!=null)

myService.setName("NAME");

} catch (RemoteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

break;

case R.id.button4:

try {

if(myService!=null)

myService.getName();

} catch (RemoteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

break;

}

}

}

  最後别忘了服務要在主配置配置一下

        <service

            android:name="com.pengxiaolog.aidl.MyService"

            android:exported="true" >

            <intent-filter>

                <action android:name="COM.PENGXIAOLONG.STYLE" />

            </intent-filter>

        </service>