版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lijia1201900857/article/details/76060544
以前在Eclipse 中写过aidl 。最近想在Android Studio 中实现以下。
我先写下我遇到的一个坑,困扰了我一下午才解决。
在所有的代码都写好后,运行程序一点反应都没有。
最后发现是权限的问题。我的是6.0的手机,默认是不支持关联启动的
需要去手机 设置--》应用程序---》权限管理 ---》选择你的服务应用 打开关联启动
开始:
先进行准备工作
新创建一个应用 如:AidlDemo 该应用作为客户端
在该应用中创建一个Moudle AidlService 作为服务端
/2、
目录结构如下:
2、开始写服务端
1、创建文件AIDL 名称为 IMyAidlInterface
会在 main目录下生成如下文件
在
IMyAidlInterface.aidl 中
写两个方法 add print
然后 Build 一下,这样就生成了java接口文件,地址在项目文件夹/app/build/generated/aidl里面
aidlservice 创建一个服务
public class AidlDemoService extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.e("dd", "service------onCreate");
}
@Override
public void unbindService(ServiceConnection conn) {
super.unbindService(conn);
Log.e("dd", "service------unbindService");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("dd", "service------onDestroy");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e("dd", "service------onBind");
return stub;
}
public IMyAidlInterface.Stub stub = new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public int add(int x, int y) throws RemoteException {
return x + y;
}
@Override
public String printStr(String str) throws RemoteException {
return str;
}
};
}
manifest.xml 中添加配置信息
action 是用来启动你的服务的
服务端完成。
客户端:
1、把aidlService 中的aidl文件夹整个拷贝到 app中
2、编写MainActivity ,进行逻辑实现
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private IMyAidlInterface anInterface;
private Button addBtn;
private Button printBtn;
private Button bindBtn;
private Button unBindBtn;
private ServiceConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
printBtn = (Button) findViewById(R.id.print_btn);
addBtn = (Button) findViewById(R.id.add_btn);
bindBtn = (Button) findViewById(R.id.bind_btn);
unBindBtn = (Button) findViewById(R.id.unBind_btn);
printBtn.setOnClickListener(this);
addBtn.setOnClickListener(this);
bindBtn.setOnClickListener(this);
unBindBtn.setOnClickListener(this);
connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//服务绑定的时候走这里
Log.e("dd", "activity------onServiceConnected");
anInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// 当内存不足或者服务异常关闭的时候走这里(解绑是不走这个方法的)
Log.e("dd", "activity------onServiceDisconnected");
anInterface = null;
}
};
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.print_btn: // print
if (anInterface == null) {
return;
}
try {
// printStr 方法
String aaa = anInterface.printStr("helloWorld");
printBtn.setText("" + aaa);
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.add_btn: // add
if (anInterface == null) {
return;
}
try {
int a = anInterface.add(10, 15);
addBtn.setText("" + a);
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.bind_btn:// 绑定服务
Toast.makeText(MainActivity.this, "bind", Toast.LENGTH_SHORT).show();
bind();
break;
case R.id.unBind_btn:// 解除绑定
Toast.makeText(MainActivity.this, "unBind", Toast.LENGTH_SHORT).show();
unBind();
break;
}
}
/**
* 绑定服务
*/
private void bind() {
Intent intent = new Intent();
intent.setAction("com.example.aidlservice.AidlDemoService");
// 5.0以上需要设置 否则报错
intent.setPackage("com.example.aidlservice");
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
/**
* 解绑服务
*/
private void unBind() {
if(connection!=null){
unbindService(connection);
}
}
}
结束:见证奇迹的时刻将要来来临
首先把两个应用都跑到手机上
点击绑定,然后 add print 成功输出。