aidl 中通过RemoteCallbackList 运用到的回调机制: service回调 android aidl 回调

说明:我没有写实例代码,直接拿项目中的代码,有点懒了,这里我省略贴出两个aidl文件。

TtsService extends Service

[java]view plaincopy

privatefinalRemoteCallbackList<ITtsCallback>mCallbacks

=newRemoteCallbackList<ITtsCallback>();

[java]view plaincopy

privatefinalandroid.speech.tts.ITts.StubmBinder=newStub(){

publicintregisterCallback(StringpackageName,ITtsCallbackcb){

if(cb!=null){

mCallbacks.register(cb);

mCallbacksMap.put(packageName,cb);

returnTextToSpeech.SUCCESS;

}

returnTextToSpeech.ERROR;

}

publicintunregisterCallback(StringpackageName,ITtsCallbackcb){

if(cb!=null){

mCallbacksMap.remove(packageName);

mCallbacks.unregister(cb);

returnTextToSpeech.SUCCESS;

}

returnTextToSpeech.ERROR;

}

publicintspeak(StringcallingApp,Stringtext,intqueueMode,String[]params){

ArrayList<String>speakingParams=newArrayList<String>();

if(params!=null){

speakingParams=newArrayList<String>(Arrays.asList(params));

}

returnthis.speak(callingApp,text,queueMode,speakingParams);

}

[java]view plaincopy

privatevoiddispatchProcessingCompletedCallback(StringpackageName){

ITtsCallbackcb=mCallbacksMap.get(packageName);

if(cb==null){

return;

}

//Log.i("TtsService","TTScallback:dispatchstarted");

//Broadcasttoallclientsthenewvalue.

finalintN=mCallbacks.beginBroadcast();

try{

cb.processingCompleted();

}catch(RemoteExceptione){

//TheRemoteCallbackListwilltakecareofremoving

//thedeadobjectforus.

}

mCallbacks.finishBroadcast();

//Log.i("TtsService","TTScallback:dispatchcompletedto"+N);

}

[java]view plaincopy

@Override

publicvoidonDestroy(){

super.onDestroy();

//TODOreplacethecalltostopAll()withamethodtoclearabsolutelyallupcoming

//usesofthenativesynth,includingsynthesistoafile,anddeletefilesforwhich

//synthesiswasnotcomplete.

stopAll();

//Unregisterallcallbacks.

mCallbacks.kill();

}

在activity中

[java]view plaincopy

mITtscallback=newITtsCallback.Stub(){

publicvoidprocessingCompleted()throwsRemoteException{

if(listener!=null){

listener.onProcessingCompleted();

}

}

};

result=mITts.registerCallback(mPackageName,mITtscallback);

上面只是一个贴代码没有做任何说明,基本的意思我想大家也能通过代码来看懂。

[java]view plaincopy

//intN=mCallbacks.beginBroadcast();

//try{

//for(inti=0;i<N;i++){

//mCallbacks.getBroadcastItem(i).showResult(mSlot);

//}

//}catch(RemoteExceptione){

//Log(""+e);

//}

//mCallbacks.finishBroadcast();

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

上传一个写的工作中用到的demo

[java]view plaincopy

packagecom.pateo.aidl;

interfaceICallback{

voidshowResult(Stringresult);

}

packagecom.pateo.aidl;

importcom.pateo.aidl.ICallback;

interfaceIMyService{

voidinit(StringpackageName,Stringslot);

voidregisterCallback(StringpackageName,ICallbackcb);

voidunregisterCallback(StringpackageName,ICallbackcb);

}

[java]view plaincopy

packagecom.pateo.service;

importjava.util.HashMap;

importandroid.app.Service;

importandroid.content.Intent;

importandroid.os.Handler;

importandroid.os.IBinder;

importandroid.os.Message;

importandroid.os.RemoteCallbackList;

importandroid.os.RemoteException;

importcom.pateo.aidl.ICallback;

importcom.pateo.aidl.IMyService;

publicclassVuiServiceextendsService{

privateRemoteCallbackList<ICallback>mCallbacks=newRemoteCallbackList<ICallback>();

privateHashMap<String,ICallback>mCallbacksMap=newHashMap<String,ICallback>();

privateStringmSlot="";

privateStringmPackageName="";

@Override

publicvoidonStart(Intentintent,intstartId){

super.onStart(intent,startId);

}

@Override

publicIBinderonBind(Intentintent){

returnremoteBinder;

}

@Override

publicvoidonDestroy(){

mHandler.removeMessages(0);

mCallbacks.kill();

super.onDestroy();

}

@Override

publicbooleanonUnbind(Intentintent){

returnsuper.onUnbind(intent);

}

publicvoidonRebind(Intentintent){

super.onRebind(intent);

}

privateIMyService.StubremoteBinder=newIMyService.Stub(){

@Override

publicvoidinit(StringpackageName,Stringslot)throwsRemoteException{

mSlot=slot;

mPackageName=packageName;

//?£?a?aê????ˉê?±e£??aà?μ?4000oá???àμ±óú??óèμ?ê?±e1y3ìμ?ê±??£??a???éò??úê?±e?á1?à???è¥μ÷ó?

mHandler.sendEmptyMessageDelayed(0,4000);

}

@Override

publicvoidunregisterCallback(StringpackageName,ICallbackcb){

if(cb!=null){

mCallbacks.unregister(cb);

mCallbacksMap.remove(packageName);

}

}

//°ü??×¢2áμ?·?ê?£??a?ù?í±ü?aá?oü?àó?ó?×¢2á??è¥??μ÷£??aà?í?1yó?ó???óèμ?packageNameà??¥????ì???μ÷??ò???ó?ó?μ?callback

@Override

publicvoidregisterCallback(StringpackageName,ICallbackcb){

if(cb!=null){

mCallbacks.register(cb);

mCallbacksMap.put(packageName,cb);

}

}

};

//?aà?í?1yó?ó???óèμ?packageNameà??¥????ì???μ÷??ò???ó?ó?μ?callback

privatevoiddispatchProcessingCompletedCallback(){

ICallbackcb=mCallbacksMap.get(mPackageName);

if(cb==null){

return;

}

finalintN=mCallbacks.beginBroadcast();

try{

cb.showResult(mSlot);

}catch(RemoteExceptione){

}

mCallbacks.finishBroadcast();

}

privateHandlermHandler=newHandler(){

@Override

publicvoidhandleMessage(Messagemsg){

dispatchProcessingCompletedCallback();

super.handleMessage(msg);

}

};

}

[java]view plaincopy

packagecom.pateo;

importcom.pateo.service.VuiService;

importcom.pateo.aidl.ICallback;

importcom.pateo.aidl.IMyService;

importcom.pateo.R;

importandroid.app.Activity;

importandroid.content.ComponentName;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.content.ServiceConnection;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.IBinder;

importandroid.os.Message;

importandroid.os.RemoteException;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.TextView;

publicclassAppActivityextendsActivity{

TextViewtv;

IMyServicemyservice;

StringmResult;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tv=(TextView)findViewById(R.id.tv);

//?£?a°?mode?ü

Buttonbtn=(Button)findViewById(R.id.startBtn);

btn.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

Intentintent=newIntent(AppActivity.this,VuiService.class);

bindService(intent,serviceConnection,Context.BIND_AUTO_CREATE);

}

});

}

privateServiceConnectionserviceConnection=newServiceConnection(){

@Override

publicvoidonServiceDisconnected(ComponentNamename){

myservice=null;

}

@Override

publicvoidonServiceConnected(ComponentNamename,IBinderservice){

myservice=IMyService.Stub.asInterface(service);

try{

if(myservice!=null){

myservice.registerCallback("com.pateo",mCallback);

myservice.init("com.pateo","??ìy|????");//?aá?ó?ò??ò?ü?óê?μ??aD??êoó°??aá????ê2?è?2?2?£??a?ù?í?éò?·??§D?ìá??ê?±e

//?aà??1?éò?×?°?ò????êì?ò2·¢1y襣?2?è?2?2??£

}

}catch(RemoteExceptione){

}

}

};

/**

*serviceμ???μ÷·?·?

*/

privateICallback.StubmCallback=newICallback.Stub(){

//μè?yê?±e?á1?è?oóshow3?à?

@Override

aidl 中通过RemoteCallbackList 运用到的回调机制: service回调 android aidl 回调
publicvoidshowResult(Stringresult){

try{

mResult=result;

Messagemsgget=Message.obtain();

msgget.what=1;

mHandler.sendMessage(msgget);

}catch(Exceptione){

}

}

};

privateHandlermHandler=newHandler(){

@Override

publicvoidhandleMessage(Messagemsg){

super.handleMessage(msg);

switch(msg.what){

case1:

tv.setText("result:"+mResult);

break;

}

}

};

@Override

protectedvoidonDestroy(){

unbindService(serviceConnection);

super.onDestroy();

}

}

来源:http://blog.csdn.net/jianguo_liao19840726/article/details/6729598

  

爱华网本文地址 » http://www.aihuau.com/a/25101011/50951.html

更多阅读

编定标书中常用到的措施费取费率 标书中的保密措施

编定标书中常用到的措施费取费标准专业不同施工组织措施费取费标准也是不同的,以下是建筑工程的施工组织措施费取费标准:%环境保护费 人工费+机械费 0.1-0.2文明施工费 人工费+机械费 0.5-0.9(非市区工程)0.9-1.4(市区一般工程)1.4-2.

声明:《aidl 中通过RemoteCallbackList 运用到的回调机制: service回调 android aidl 回调》为网友不帅你报警分享!如侵犯到您的合法权益请联系我们删除