android多个app之间通讯,android aidl通讯两个APP之间的应用

一:数据提供端代码(数据提供APP)

1.java代码:Service

package com.example.remoteaidi;

import com.example.remoteaidi.aidl.StudentAidl;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.os.RemoteException;

public class StudentService extends Service {

private String[] studentNo = {"王1","2","王3","王4","王月4","宋6"};

private IBinder binder = new StudentQueryBinder();

@Override

public IBinder onBind(Intent intent) {

return binder;

}

class StudentQueryBinder extends StudentAidl.Stub {

@Override

public String getStudent(int no) throws RemoteException {

int l = studentNo.length;

if(l<0) {

no = 0;

}

if(no>=l) {

no = l - 1;

}

return studentNo[no];

}

}

}aidl文件

package com.example.remoteaidi.aidl;

interface StudentAidl {

String getStudent(int no);

}

清单文件

二:请求端代码

package com.example.aidlclient;

import com.example.remoteaidi.aidl.StudentAidl;

import android.app.Activity;

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

import android.widget.TextView;

public class MainActivity extends Activity {

private ServiceConnectionImpl sci ;

private StudentAidl student;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Intent intent = new Intent();

intent.setAction("student.query");

sci = new ServiceConnectionImpl();

bindService(intent, sci, BIND_AUTO_CREATE);

}

public void queryOnClick(View v) {

EditText et_no_text = (EditText) findViewById(R.id.et_no_text);

String no = et_no_text.getText().toString();

String sname;

try {

sname = student.getStudent(new Integer(no));

TextView tv_no = (TextView) findViewById(R.id.tv_no);

tv_no.setText(sname);

} catch (NumberFormatException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (RemoteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

class ServiceConnectionImpl implements ServiceConnection {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

student = StudentAidl.Stub.asInterface(service);

}

@Override

public void onServiceDisconnected(ComponentName name) {

student = null;

}

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

if(sci != null) {

unbindService(sci);

}

}

}

aidl文件与上一个aidl文件相同

package com.example.remoteaidi.aidl;

interface StudentAidl {

String getStudent(int no);

}

布局文件

清单文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值