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>

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值