在AIDL文件里面添加方法:
void setData(String data);
使用的時(shí)候需要實(shí)現(xiàn)這個(gè)接口修械,
public class AppService extends Service {
public AppService() {
}
@Override
public IBinder onBind(Intent intent) {
return new IAppServiceRemoteBindler.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 int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service start");
new Thread(){
@Override
public void run() {
super.run();
running = true;
while (running){
System.out.println(data);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("service destroyed");
running = false;
}
private String data = "默認(rèn)數(shù)據(jù)";
private boolean running = false;
}
要想通過Binder去執(zhí)行另外一個(gè)App里面AIDL的函數(shù)瓤帚,需要把App里面的AIDL文件拷貝到AnotherApp中博助,并且包名要和App中一樣(先創(chuàng)建一個(gè)AIDL的folder眨唬,然后創(chuàng)建一個(gè)包什往,包名和App里面AIDL包名字一樣包蓝,然后在創(chuàng)建一個(gè)一樣的AIDL文件)糖权。
之后就可以在AnotherApp中使用這個(gè)AIDL厕隧,以及里面的方法:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
private Intent serviceIntent;
private EditText etInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceIntent = new Intent();
serviceIntent.setComponent(new ComponentName("com.chenshipeng.startservicefromanotherapp","com.chenshipeng.startservicefromanotherapp.AppService"));
etInput = (EditText)findViewById(R.id.etInput);
findViewById(R.id.btnStartAppService).setOnClickListener(this);
findViewById(R.id.btnStopAppService).setOnClickListener(this);
findViewById(R.id.bindAppService).setOnClickListener(this);
findViewById(R.id.unBindAppService).setOnClickListener(this);
findViewById(R.id.btnSync).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnStartAppService:
startService(serviceIntent);
break;
case R.id.btnStopAppService:
stopService(serviceIntent);
break;
case R.id.bindAppService:
bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);
break;
case R.id.unBindAppService:
unbindService(this);
bindler = null;
break;
case R.id.btnSync:
if (bindler != null){
try {
bindler.setData(etInput.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
break;
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("bind service");
System.out.println(service);
//這里不能直接進(jìn)行強(qiáng)制類型轉(zhuǎn)換奔脐,雖然名字一樣,但是是在兩個(gè)App鐘吁讨,內(nèi)存地址是不一樣的髓迎,所以用以下這種轉(zhuǎn)換方式。
bindler = IAppServiceRemoteBindler.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
private IAppServiceRemoteBindler bindler = null;
}