![](/img/trans.png)
[英]Java Printer Service throws ClassCastException while using PrinterURI Attribute
[英]ClassCastException while using service
我定義了一個本地服務:
public class ComService extends Service implements IComService {
private IBinder binder = new ComServiceBinder();
public class ComServiceBinder extends Binder implements IComService.IServiceBinder {
public IComService getService() {
return ComService.this;
}
}
public void test(String msg) {
System.out.println(msg);
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
}
相應的界面:
public interface IComService {
public void test(String msg);
public interface IServiceBinder {
IComService getService();
}
}
然后我嘗試將服務綁定到另一個應用程序中的另一個活動中,其中相同的接口可用:
bindService(new Intent("ch.ifi.csg.games4blue.gamebase.api.ComService"), conn, Context.BIND_AUTO_CREATE);
和
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("INFO", "Service bound " + name);
comService = ((IComService.IServiceBinder)service).getService();
serviceHandler.sendEmptyMessage(0);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
Log.i("INFO", "Service Unbound ");
}
};
但是這條線
comService = ((IComService.IServiceBinder)service).getService();
總是拋出一個
05-02 22:12:55.922: ERROR/AndroidRuntime(622): java.lang.ClassCastException: android.os.BinderProxy
我無法解釋原因,我在http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalServiceBinding.html上關注了應用示例
任何提示都會很好!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.