清单文件
<service
android:name=".doubleservice.TwoService"
android:process="headlinenews.two"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.two"></action>
</intent-filter>
</service>
<service
android:name=".doubleservice.OneService"
android:process="headlinenews.one"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.one"></action>
</intent-filter>
</service>
创建aidl文件
interface IMyAidlInterface {
String getName();
void getTime();
}
One
package com.example.headlinenews.doubleservice;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.example.headlinenews.IMyAidlInterface;
public class OneService extends Service {
public OneService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d("zyb", "OneService onCreate: ");
Intent intent = new Intent();
intent.setAction("com.example.two");
intent.setPackage("com.example.headlinenews");
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
}
@Override
public IBinder onBind(Intent intent) {
return new OneBinder();
}
private class OneBinder extends IMyAidlInterface.Stub{
@Override
public String getName() throws RemoteException {
return "one";
}
@Override
public void getTime() throws RemoteException {
Log.d("zyb", "OneBinder getTime: ");
}
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
Log.d("zyb", "onServiceConnected: one"+iMyAidlInterface.getName());
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("zyb", "OneService onServiceDisconnected: ");
Intent intent = new Intent();
intent.setAction("com.example.two");
intent.setPackage("com.example.headlinenews");
startService(intent);
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
}
};
}
Two
package com.example.headlinenews.doubleservice;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.example.headlinenews.IMyAidlInterface;
import com.example.headlinenews.service.ProgressService;
public class TwoService extends Service {
public TwoService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d("zyb", "TwoService onCreate: ");
Intent intent = new Intent();
intent.setAction("com.example.one");
intent.setPackage("com.example.headlinenews");
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
}
@Override
public IBinder onBind(Intent intent) {
return new TwoBinder();
}
private class TwoBinder extends IMyAidlInterface.Stub{
@Override
public String getName() throws RemoteException {
Log.d("zyb", "TwoBinder getName: ");
return "two";
}
@Override
public void getTime() throws RemoteException {
Log.d("zyb", "TwoBinder getTime: ");
}
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
Log.d("zyb", "onServiceConnected: two"+iMyAidlInterface.getName());
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Intent intent = new Intent("com.example.one");
intent.setPackage("com.example.headlinenews");
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
startService(intent);
Log.d("zyb", "TwoService onServiceDisconnected: ");
}
};
}