Android(安卓)AIDL介绍
16lz
2021-01-26
- packagecom.android.aidltest;
- importcom.android.aidltest.ITaskCallback;
- interfaceITaskBinder{
- booleanisTaskRunning();
- voidstopRunningTask();
- voidregisterCallback(ITaskCallbackcb);
- voidunregisterCallback(ITaskCallbackcb);
- }
- packagecom.android.aidltest;
- interfaceITaskCallback{
- voidactionPerformed(intactionId);
- }
- privatefinalITaskBinder.StubmBinder=newITaskBinder.Stub(){
- @Override
- publicvoidunregisterCallback(ITaskCallbackcb)throwsRemoteException{
- printf("serviceonunregisterCallback");
- //TODOAuto-generatedmethodstub
- if(cb!=null)
- mCallbacks.unregister(cb);
- }
- @Override
- publicvoidstopRunningTask()throwsRemoteException{
- printf("serviceonstopRunningTask");
- //TODOAuto-generatedmethodstub
- }
- @Override
- publicvoidregisterCallback(ITaskCallbackcb)throwsRemoteException{
- printf("serviceonregisterCallback");
- //TODOAuto-generatedmethodstub
- if(cb!=null)
- mCallbacks.register(cb);
- }
- @Override
- publicbooleanisTaskRunning()throwsRemoteException{
- printf("serviceonisTaskRunning");
- //TODOAuto-generatedmethodstub
- returnfalse;
- }
- };
- privateITaskCallbackmCallback=newITaskCallback.Stub(){
- @Override
- publicvoidactionPerformed(intactionId)throwsRemoteException{
- //TODOAuto-generatedmethodstub
- printf("callbackid="+actionId);
- }
- };
publicIBinderonBind(Intentt){
printf("serviceonbind");
returnmBinder;
- voidcallback(intval){
- finalintN=mCallbacks.beginBroadcast();
- for(inti=0;i<N;i++){
- try{
- mCallbacks.getBroadcastItem(i).actionPerformed(val);
- }catch(RemoteExceptione){
- }
- }
- mCallbacks.finishBroadcast();
- }
- packagecom.android.aidltest;
- interfaceITaskCallback{
- voidactionPerformed(intactionId);
- }
- packagecom.android.aidltest;
- importcom.android.aidltest.ITaskCallback;
- interfaceITaskBinder{
- booleanisTaskRunning();
- voidstopRunningTask();
- voidregisterCallback(ITaskCallbackcb);
- voidunregisterCallback(ITaskCallbackcb);
- }
- packagecom.android.aidltest;
- importandroid.app.Service;
- importandroid.content.Intent;
- importandroid.os.IBinder;
- importandroid.os.RemoteCallbackList;
- importandroid.os.RemoteException;
- importandroid.util.Log;
- publicclassMyServiceextendsService{
- publicvoidonCreate(){
- printf("servicecreate");
- }
- publicvoidonStart(Intentintent,intstartId){
- printf("servicestartid="+startId);
- callback(startId);
- }
- @Override
- publicIBinderonBind(Intentintent){
- //TODOAuto-generatedmethodstub
- printf("serviceonbind");
- returnmBinder;
- }
- publicvoidonDestroy(){
- printf("serviceondestroy");
- super.onDestroy();
- }
- publicbooleanonUnbind(Intentintent){
- printf("serviceonunbind");
- returnsuper.onUnbind(intent);
- }
- publicvoidonRebind(Intentintent){
- printf("serviceonrebind");
- super.onRebind(intent);
- }
- privatevoidprintf(Stringstr){
- Log.e("TAG","#######################---"+str+"-------");
- }
- voidcallback(intval){
- finalintN=mCallbacks.beginBroadcast();
- for(inti=0;i<N;i++){
- try{
- mCallbacks.getBroadcastItem(i).actionPerformed(val);
- }catch(RemoteExceptione){
- }
- }
- mCallbacks.finishBroadcast();
- }
- privatefinalITaskBinder.StubmBinder=newITaskBinder.Stub(){
- @Override
- publicvoidunregisterCallback(ITaskCallbackcb)throwsRemoteException{
- printf("serviceonunregisterCallback");
- //TODOAuto-generatedmethodstub
- if(cb!=null)
- mCallbacks.unregister(cb);
- }
- @Override
- publicvoidstopRunningTask()throwsRemoteException{
- printf("serviceonstopRunningTask");
- //TODOAuto-generatedmethodstub
- }
- @Override
- publicvoidregisterCallback(ITaskCallbackcb)throwsRemoteException{
- printf("serviceonregisterCallback");
- //TODOAuto-generatedmethodstub
- if(cb!=null)
- mCallbacks.register(cb);
- }
- @Override
- publicbooleanisTaskRunning()throwsRemoteException{
- printf("serviceonisTaskRunning");
- //TODOAuto-generatedmethodstub
- returnfalse;
- }
- };
- finalRemoteCallbackList<ITaskCallback>mCallbacks
- =newRemoteCallbackList<ITaskCallback>();
- }
- packagecom.android.aidltest;
- importandroid.app.Activity;
- importandroid.content.ComponentName;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.content.ServiceConnection;
- importandroid.os.Bundle;
- importandroid.os.IBinder;
- importandroid.os.RemoteException;
- importandroid.util.Log;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- publicclassAidlTestextendsActivity{
- privateButtonbtnOk;
- privateButtonbtnCancel;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btnOk=(Button)findViewById(R.id.btn_ok);
- btnCancel=(Button)findViewById(R.id.btn_cancel);
- btnOk.setText("StartService");
- btnCancel.setText("StopService");
- btnOk.setOnClickListener(newOnClickListener(){
- publicvoidonClick(Viewv){
- onOkClick();
- }
- });
- btnCancel.setOnClickListener(newOnClickListener(){
- publicvoidonClick(Viewv){
- onCancelClick();
- }
- });
- }
- voidonOkClick(){
- printf("clickedok");
- Bundleargs=newBundle();
- Intentintent=newIntent(this,MyService.class);
- intent.putExtras(args);
- bindService(intent,mConnection,Context.BIND_AUTO_CREATE);
- startService(intent);
- }
- voidonCancelClick(){
- printf("clickedcancel");
- Intentintent=newIntent(this,MyService.class);
- unbindService(mConnection);
- }
- privatevoidprintf(Stringstr){
- Log.e("TAG","##################-------"+str+"-----");
- }
- ITaskBindermService;
- privateServiceConnectionmConnection=newServiceConnection(){
- publicvoidonServiceConnected(ComponentNameclassName,
- IBinderservice){
- mService=ITaskBinder.Stub.asInterface(service);
- try{
- mService.registerCallback(mCallback);
- }catch(RemoteExceptione){
- }
- }
- publicvoidonServiceDisconnected(ComponentNameclassName){
- mService=null;
- }
- };
- privateITaskCallbackmCallback=newITaskCallback.Stub(){
- @Override
- publicvoidactionPerformed(intactionId)throwsRemoteException{
- //TODOAuto-generatedmethodstub
- printf("callbackid="+actionId);
- }
- };
- }
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.aidltest"
- android:versionCode="1"
- android:versionName="1.0">
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".AidlTest"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <serviceandroid:name=".MyService">
- <intent-filter>
- <actionandroid:name="com.android.aidltest.START_MYSERVICE"/>
- <categoryandroid:name="android.intent.category.DEFAULT"/>
- </intent-filter>
- </service>
- </application>
- <uses-sdkandroid:minSdkVersion="8"/>
- </manifest>
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/btn_ok"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn_ok"
- />
- <Button
- android:id="@+id/btn_cancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn_cancel"
- />
- </LinearLayout>
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <stringname="hello">HelloWorld,AidlTest!</string>
- <stringname="app_name">AIDLTest</string>
- <stringname="btn_ok">OK</string>
- <stringname="btn_cancel">Cancel</string>
- </resources>
更多相关文章
- Android(安卓)Fragment 解析
- Android(安卓)OkHttp完全解析 是时候来了解OkHttp了
- Android(安卓)Studio 文件提前结束
- Android实现拍照及图片显示效果
- 二,HelloWorld 及源码关联 & Manifest 类关联
- WebView用法与JS交互
- 初学Android,使用Drawable资源之使用StateListDrawable资源(十三
- Android(安卓)中音频视频开发
- AIDL和远程Service调用