OnServiceConnected没有得到所谓的 [英] OnServiceConnected not getting called
问题描述
我下文称<一个href=\"http://stackoverflow.com/questions/6008601/cant-get-service-object-onserviceconnected-never-called\">Can't让服务对象(onServiceConnected从来不叫),<一个href=\"http://stackoverflow.com/questions/11844920/onserviceconnected-not-getting-called-getting-a-null-pointer-exception\">onServiceConnected没有得到所谓的,得到一个空指针异常和<一个href=\"http://stackoverflow.com/questions/2486692/onserviceconnected-never-called-after-bindservice-method\">onServiceConnected bindService方法后就再也没打来电话,没有找到答案。
I reffered Can't get service object (onServiceConnected never called) , onServiceConnected not getting called , getting a null pointer exception and onServiceConnected never called after bindService method and not found answer.
这是我的code
@Override
public void onStart() {
super.onStart();
Context context = getApplicationContext();
Intent intent = new Intent(context, PodService.class);
context.bindService(intent, mPodServiceConn, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mPodServiceConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
Log.e(TAG, "Pod: Service Connected");
mPodService = IPodService.Stub.asInterface(service); //here i am getting NullPointerException
}
}
和我的服务类包含什么都没有,只有这个了,我已经展示了它吼叫
and My service class contains nothing, only this much, i have shown it bellow
public class PodService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.d("bound", "bound");
return null;
}
static class PodSeviceStub extends IPodService.Stub {
//here i implemented unimplemented methods
}
}
但在lolcat我只收到约束
从onBind消息()函数,而不是印刷波德:服务连接
,是指服务启动成功。
在lolcat我收到 NullPointerException异常
,并在清单文件中也提到过。
But in lolcat i am getting only "bound
" message from onBind() function, but not printing "Pod: Service Connected
", means service is started successfully.
and in lolcat i am getting NullPointerException
and also mentioned in manifest file too.
推荐答案
我改写了服务类,这样onBind()返回的IBinder为follwos。
I rewrote the service class so that onBind() returns IBinder as follwos.
public class PodService extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.d("bound", "bound");
return mBinder; // returns IBinder object
}
private final IBinder mBinder = new PodSeviceStub(this);
static class PodSeviceStub extends IPodService.Stub {
WeakReference<PodService> mService;
public PodSeviceStub(PodService service) {// added a constructor for Stub here
mService = new WeakReference<PodService>(service);
}
//here i implemented unimplemented methods
}
}
和现在的作品。
这篇关于OnServiceConnected没有得到所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!