打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
aidl 中通过RemoteCallbackList 运用到的回调机制: service回调activity的方法
说明:我没有写实例代码,直接拿项目中的代码,有点懒了,这里我省略贴出两个aidl文件。
TtsService extends Service
[java] view plaincopy
private final RemoteCallbackList<ITtsCallback> mCallbacks
= new RemoteCallbackList<ITtsCallback>();
[java] view plaincopy
private final android.speech.tts.ITts.Stub mBinder = new Stub() {
public int registerCallback(String packageName, ITtsCallback cb) {
if (cb != null) {
mCallbacks.register(cb);
mCallbacksMap.put(packageName, cb);
return TextToSpeech.SUCCESS;
}
return TextToSpeech.ERROR;
}
public int unregisterCallback(String packageName, ITtsCallback cb) {
if (cb != null) {
mCallbacksMap.remove(packageName);
mCallbacks.unregister(cb);
return TextToSpeech.SUCCESS;
}
return TextToSpeech.ERROR;
}
public int speak(String callingApp, String text, int queueMode, String[] params) {
ArrayList<String> speakingParams = new ArrayList<String>();
if (params != null) {
speakingParams = new ArrayList<String>(Arrays.asList(params));
}
return this.speak(callingApp, text, queueMode, speakingParams);
}
[java] view plaincopy
private void dispatchProcessingCompletedCallback(String packageName) {
ITtsCallback cb = mCallbacksMap.get(packageName);
if (cb == null){
return;
}
//Log.i("TtsService", "TTS callback: dispatch started");
// Broadcast to all clients the new value.
final int N = mCallbacks.beginBroadcast();
try {
cb.processingCompleted();
} catch (RemoteException e) {
// The RemoteCallbackList will take care of removing
// the dead object for us.
}
mCallbacks.finishBroadcast();
//Log.i("TtsService", "TTS callback: dispatch completed to " + N);
}
[java] view plaincopy
@Override
public void onDestroy() {
super.onDestroy();
// TODO replace the call to stopAll() with a method to clear absolutely all upcoming
// uses of the native synth, including synthesis to a file, and delete files for which
// synthesis was not complete.
stopAll();
// Unregister all callbacks.
mCallbacks.kill();
}
在activity中
[java] view plaincopy
mITtscallback = new ITtsCallback.Stub() {
public void processingCompleted() throws RemoteException {
if (listener != null) {
listener.onProcessingCompleted();
}
}
};
result = mITts.registerCallback(mPackageName, mITtscallback);
上面只是一个贴代码没有做任何说明,基本的意思我想大家也能通过代码来看懂。
[java] view plaincopy
// int N = mCallbacks.beginBroadcast();
// try {
// for (int i = 0; i < N; i++) {
// mCallbacks.getBroadcastItem(i).showResult(mSlot);
// }
// } catch (RemoteException e) {
// Log("" + e);
// }
// mCallbacks.finishBroadcast();
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
上传一个写的工作中用到的demo
[java] view plaincopy
package com.pateo.aidl;
interface ICallback {
void showResult(String result);
}
package com.pateo.aidl;
import com.pateo.aidl.ICallback;
interface IMyService {
void init(String packageName,String slot);
void registerCallback(String packageName,ICallback cb);
void unregisterCallback(String packageName,ICallback cb);
}
[java] view plaincopy
package com.pateo.service;
import java.util.HashMap;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import com.pateo.aidl.ICallback;
import com.pateo.aidl.IMyService;
public class VuiService extends Service {
private RemoteCallbackList<ICallback> mCallbacks = new RemoteCallbackList<ICallback>();
private HashMap<String, ICallback> mCallbacksMap = new HashMap<String, ICallback>();
private String mSlot = "";
private String mPackageName = "";
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return remoteBinder;
}
@Override
public void onDestroy() {
mHandler.removeMessages(0);
mCallbacks.kill();
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
public void onRebind(Intent intent) {
super.onRebind(intent);
}
private IMyService.Stub remoteBinder = new IMyService.Stub() {
@Override
public void init(String packageName,String slot) throws RemoteException {
mSlot = slot;
mPackageName = packageName;
//?£?a?aê????ˉê?±e£??aà?μ?4000oá???àμ±óú??óèμ?ê?±e1y3ìμ?ê±??£??a???éò??úê?±e?á1?à???è¥μ÷ó?
mHandler.sendEmptyMessageDelayed(0, 4000);
}
@Override
public void unregisterCallback(String packageName, ICallback cb) {
if (cb != null) {
mCallbacks.unregister(cb);
mCallbacksMap.remove(packageName);
}
}
//°ü??×¢2áμ?·?ê?£??a?ù?í±ü?aá?oü?àó?ó?×¢2á??è¥??μ÷£??aà?í?1yó?ó???óèμ?packageNameà??¥????ì???μ÷??ò???ó?ó?μ?callback
@Override
public void registerCallback(String packageName, ICallback cb) {
if (cb != null) {
mCallbacks.register(cb);
mCallbacksMap.put(packageName, cb);
}
}
};
//?aà?í?1yó?ó???óèμ?packageNameà??¥????ì???μ÷??ò???ó?ó?μ?callback
private void dispatchProcessingCompletedCallback() {
ICallback cb = mCallbacksMap.get(mPackageName);
if (cb == null){
return;
}
final int N = mCallbacks.beginBroadcast();
try {
cb.showResult(mSlot);
} catch (RemoteException e) {
}
mCallbacks.finishBroadcast();
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
dispatchProcessingCompletedCallback();
super.handleMessage(msg);
}
};
}
[java] view plaincopy
package com.pateo;
import com.pateo.service.VuiService;
import com.pateo.aidl.ICallback;
import com.pateo.aidl.IMyService;
import com.pateo.R;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AppActivity extends Activity {
TextView tv;
IMyService myservice ;
String mResult ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
//?£?a°?mode?ü
Button btn =  (Button) findViewById(R.id.startBtn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AppActivity.this,VuiService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
});
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
myservice = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
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 (RemoteException e) {
}
}
};
/**
* serviceμ???μ÷·?·?
*/
private ICallback.Stub mCallback = new ICallback.Stub() {
//μè?yê?±e?á1?è?oóshow3?à?
@Override
public void showResult(String result) {
try {
mResult = result;
Message msgget = Message.obtain();
msgget.what = 1;
mHandler.sendMessage(msgget);
} catch (Exception e) {
}
}
};
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
tv.setText("result : " + mResult);
break;
}
}
};
@Override
protected void onDestroy() {
unbindService(serviceConnection);
super.onDestroy();
}
}
来源:http://blog.csdn.net/jianguo_liao19840726/article/details/6729598
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用android中的AIDL让Service与Activity通信
Android 中的AIDL
Android 进程绝杀技
[z]android 应用程序Activity之间数据传递与共享的几种途径
使用AIDL实现进程间的通信
Android AIDL(Android Interface Definition Language)介绍
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服