環(huán)境
開發(fā)工具:Android studio
整體的文件目錄
Paste_Image.png
server端編寫
1、在Java那邊新建aidl文件,會自動生成和Java并列的aidl文件夾,包名也是一致的
Paste_Image.png
會自動生成代碼
interface IMyAidlInterface {
/**
* 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);
}
不用管,加上自己的測試代碼
interface IMyAidlInterface {
/**
* 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 myTest();
}
2、新建service文件,主要就是返回IBinder對象
public class myService extends Service{
@Override
public void onCreate() {
super.onCreate();
Log.e("myService--->","onCreate");
}
private IBinder iBinder= new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void myTest() throws RemoteException {
Log.e("iiiiiiiii--->","yeyeyyeyeyeyeye");
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return iBinder;
}
}
3、注冊service
<service android:name="com.renrun.service.myService"
android:enabled="true"
android:exported="true"
android:process=":remote">
<intent-filter>
<action android:name="com.renrun.service.myService"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
android:enabled 當前Activity是否可以被另一個Application的組件啟動:true允許被啟動;false不允許被啟動。
只要有filter,默認值就是true,否則是false
android:exported 其它應用的組件是否可以喚醒service或者和這個service進行交互:true可以,false不可以
只要有filter,默認值就是true,否則是false
android:process 標志在哪個線程,:開頭表示是私有的,會補上包名,和com.renrun.service.remote相比,就是公有和私有的區(qū)別。
action android:name 表示可以通過這個名稱來啟動這個service
category android:name 是默認的模式
client端編寫
1、拷貝service端的aidl整個文件夾到同樣的地方。
2、調用服務,初始化變量
IMyAidlInterface iMyAidl;
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("cyqlog", "onServiceConnected success");
iMyAidl=IMyAidlInterface.Stub.asInterface(service); // 1
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("cyqlog", "onServicedisConnected ");
iMyAidl=null;
}
};
調用
Intent intent = new Intent();
//綁定服務端的service
intent.setAction("com.renrun.service.myService");
// intent.setPackage("com.renrun.service");
//新版本(5.0后)必須顯式intent啟動 綁定服務
intent.setComponent(new ComponentName("com.renrun.service","com.renrun.service.myService"));
startService(intent);
//綁定的時候服務端自動創(chuàng)建
bindService(intent,conn, Context.BIND_AUTO_CREATE);
Boolean b = bindService(intent,conn, Context.BIND_AUTO_CREATE);
Log.e("i00---00-90------>","1-->"+b);
new Handler().postDelayed(new Runnable(){
public void run() {
try {
iMyAidl.myTest();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}, 1000);
特別注意:
按照正常的流程startService(intent);是不需要的,
但是魅族是個奇葩,沒有startService(intent); bindService返回綁定失敗,所以通用寫法一定要加上。
測試運行
先運行server,再運行oo,能看到service日志打印。