aidl进程之间的通信

AIDL1

MainActivity

package cn.bgs.aidlc;



import cn.bgs.aidl.Server;
import cn.bgs.aidl.Server.Stub;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity implements OnClickListener {
private Button mBtn1,mBtn2;
private Conn conn;
Server m;//aidl文件的对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conn=new Conn();
initView();
}
private void initView() {
mBtn1=(Button) findViewById(R.id.mBtn1);
mBtn2=(Button) findViewById(R.id.mBtn2);


mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(R.id.mBtn1==v.getId()){
//绑定服务(服务器端的服务)  
//用隐式跳转的方式去访问另一个进程中的服务
Intent intent=new Intent();
intent.setAction("cn.bgs.wanglong");
intent.addCategory(Intent.CATEGORY_DEFAULT);
//绑定服务
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}else{
//输出绑定成功的结果
try {
m.LogE();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unbindService(conn);
}
private class Conn implements ServiceConnection{


@Override
public void onServiceConnected(ComponentName name, IBinder service) {
m=Stub.asInterface(service);
Log.e("", "AAAAAA"+m);
}


@Override
public void onServiceDisconnected(ComponentName name) {

}

}

}

XML

activity_main

<LinearLayout 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=".MainActivity" 
    android:orientation="vertical"
    >
<Button 
   android:id="@+id/mBtn1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="绑定服务"
   />
<Button 
   android:id="@+id/mBtn2"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="输出访问语句"
   />
</LinearLayout>




AIDL2

MainActivity.class

package cn.bgs.aidls;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

ServerService.class

package cn.bgs.aidls;


import cn.bgs.aidl.Server.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;


public class ServerService extends Service{


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new My();
}
private class My extends Stub{


@Override
public void LogE() throws RemoteException {
Log.e("", "AIDLS下的服务被访问");

}

}


}


XML

activity_main

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


</RelativeLayout>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值