Android AIDL语言交互

AIDL语言交互

1.6.0系统以上系统不能用,包括6.0

2.用于进程之间的通信

3.客户端使用服务端里面的东西


AIDL

package com.example.aidl;
interface AidlDemo{
String name();
String age();
}


服务端

package com.example.aidl_serverce;
import com.example.aidl.AidlDemo.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;


public class MyService extends Service {
private StudentTest student;




@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
student=new StudentTest("小明", "13");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();
}
class MyBinder extends Stub{


@Override
public String name() throws RemoteException {
// TODO Auto-generated method stub
return student.getName();
}


@Override
public String age() throws RemoteException {
// TODO Auto-generated method stub
return student.getAge();
}
}
}

建立一个bean类

package com.example.aidl_serverce;


public class StudentTest {
private String name;
private String age;
public StudentTest(String name, String age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}

客户端

package com.example.aidl_kehu;
import com.example.aidl.AidlDemo;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button mBtn;
private AidlDemo binder;
//客户端和服务端的实现,要定义ServiceConnection
private ServiceConnection conn=new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.e("", "连接失败");
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
binder=AidlDemo.Stub.asInterface(service);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initView();
}
private void initView() {
mBtn=(Button) findViewById(R.id.mBtn);
Intent intent=new Intent();
intent.setAction("com.example.aidl_serverce.aidl");
intent.setPackage("com.example.aidl_serverce");
//bindService建立连接
bindService(intent, conn,Service.BIND_AUTO_CREATE);
mBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
try {
String age = binder.age();
String name = binder.name();
Toast.makeText(MainActivity.this, "age"+age+"name"+name, Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}

客户端  xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.aidl_kehu.MainActivity" >


    <Button
        android:id="@+id/mBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击交互" />
</RelativeLayout>

配置清单Manifest    注册服务端

        <service android:name="com.example.aidl_serverce.MyService" >
            <intent-filter>
                <action android:name="com.example.aidl_serverce.aidl" />
            </intent-filter>
        </service>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值