您的位置:首页 > 移动开发 > Android开发

Android studio 使用aidl实现远程服务

2016-06-19 13:44 459 查看
AIDL
定义

在androidstudio简单使用
服务端

客户端

代码

AIDL

定义

Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口

在androidstudio简单使用

服务端

新建服务

public class TestService extends Service {
private static final String TAG = "TestService";
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind: " );
return new TextBinder();
}

class TextBinder extends  IMyAidlInterface.Stub{

@Override
public void pay(int money) throws RemoteException {
TestService.this.pay(money);
}
}
public void pay(int money){
Log.e(TAG, "pay: "+money+"sueecss" );
}
}


新建aidl文件

app右键 new->aidl

// IMyAidlInterface.aidl
package cn.zsp.aidldemo_01;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
void pay(int money);
}


Build ->mark project (生成对应的java文件)

客户端

复制服务端的aidl (包括包要完全一致)

客户端代码

package cn.zsp.aidldemo_02;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import cn.zsp.aidldemo_01.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {
private EditText etMoney;
private IMyAidlInterface mMyAidlInterface;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etMoney= (EditText) findViewById(R.id.etMoney);
Intent intent=new Intent();
intent.setAction("cn.zsp.testservice");
Intent intent1=new Intent(getExplicitIntent(this,intent));
bindService(intent1, new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
Toast.makeText(MainActivity.this, "服务连接成功", Toast.LENGTH_SHORT).show();
//服务链接成功才能进行后续操作 否者会报空指针异常  服务连接需要时间

}

@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(MainActivity.this, "服务连接失败", Toast.LENGTH_SHORT).show();
}
},BIND_AUTO_CREATE);
}

public void pay(View view){
try {
String money=etMoney.getText().toString().trim();
mMyAidlInterface.pay(Integer.parseInt(money));
} catch (RemoteException e) {
e.printStackTrace();
}
}

/**
* Android 5.0 之后 服务必须显示启动 所以需要把隐式改成显示启动
* @param context
* @param implicitIntent
* @return
*/
public static Intent getExplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
}


代码

链接:http://pan.baidu.com/s/1nvdCKFv 密码:es58
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android aidl service