gpt4 book ai didi

android - OnServiceConnected 未被调用

转载 作者:太空狗 更新时间:2023-10-29 14:23:05 31 4
gpt4 key购买 nike

我已经提到了以下问题,但找不到答案:

这是我的代码:

@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
}
}

我的服务类什么都没有,只有这么多,我已经在下面展示了

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() 函数收到“bound”消息,但没有打印“Pod: Service Connected”,这意味着服务已成功启动。在 lolcat 中,我得到了 NullPointerException 并且也在 list 文件中提到。

最佳答案

我重写了服务类,以便 onBind() 返回 IBinder 作为 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

}
}

现在可以了。

关于android - OnServiceConnected 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14936225/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com