在上两个笔记上加入了跨应用之间的绑定通信。
步骤:
1.创建一个Folder : NEW->Folder->aidl Folder
2.在该aidl 中创建一个package,包名和要启动的那个服务app中的androidmanifest.xml中的package一致
3.复制服务app中创建的aidl类到步骤2中创建的包中。
// AppServiceRemoteBinder.aidl
package com.example.yabushan.aidsservice;
// Declare any non-default types here with import statements
//AIDL接口
interface AppServiceRemoteBinder {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
//创建一个方法用于同步数据
void setData(String data);
}
package com.example.yabushan.aidsservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
//服务类
public class AppService extends Service {
public AppService() {
}
@Override
public IBinder onBind(Intent intent) {
//实现了上边的aidl接口
return new AppServiceRemoteBinder.Stub(){
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void setData(String data) throws RemoteException {
AppService.this.data=data;
}
};
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("service on start");
new Thread(){
@Override
public void run() {
super.run();
running=true;
while (running){
try {
System.out.println(data);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("service deastroy");
running =false;
}
//输出的数据
private String data="默认数据";
//控制线程的开启、关闭
private boolean running=false;
}
另一个调用服务类的app
package com.example.yabushan.anotherapp;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
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 com.example.yabushan.aidsservice.AppServiceRemoteBinder;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
private Intent serviceIntent;
private EditText input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceIntent=new Intent();
serviceIntent.setComponent(new ComponentName("com.example.yabushan.aidsservice", "com.example.yabushan.aidsservice.AppService"));
findViewById(R.id.startService).setOnClickListener(this);
findViewById(R.id.stopService).setOnClickListener(this);
findViewById(R.id.BindService).setOnClickListener(this);
findViewById(R.id.unBindService).setOnClickListener(this);
findViewById(R.id.sync).setOnClickListener(this);
input= (EditText) findViewById(R.id.et);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.startService:
startService(serviceIntent);
break;
case R.id.stopService:
stopService(serviceIntent);
break;
case R.id.BindService:
bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);
break;
case R.id.unBindService:
unbindService(this);
binder=null;
break;
case R.id.sync:
try {
binder.setData(input.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
System.out.println("无法执行远程服务");
}
break;
}
}
private AppServiceRemoteBinder binder=null;
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("bind service");
System.out.println(service);
//由于AppServiceRemoteBinder是在两个不同的应用中,所以有两个不同的地址,是不同的两个binder
//所以不能使用下面的强制转换
//binder= (AppServiceRemoteBinder) service;
binder=AppServiceRemoteBinder.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动外部服务" android:id="@+id/startService" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止外部服务" android:id="@+id/stopService" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定外部服务" android:id="@+id/BindService" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解除绑定外部服务" android:id="@+id/unBindService" /> <EditText android:layout_width="wrap_content" android:layout_height="match_parent" android:text="这是另一个app中的数据" android:id="@+id/et"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="同步数据到绑定服务中" android:id="@+id/sync"/> </LinearLayout>
目录结构: