首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中调用bindService

在android中调用bindService
EN

Stack Overflow用户
提问于 2014-07-25 11:27:09
回答 1查看 466关注 0票数 2

我正在从MyFragment.class中的onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)调用bind(),在onResume()中,我试图访问mSerive,但它显示的是空指针异常onServiceConnected(ComponentName name, IBinder service)onResume()之后调用。为什么我打电话给onServiceConnected时不调用bind(),有人能给出建议吗?

代码语言:javascript
复制
private final ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        try {
            mService.unregisterCallback(mCallback.this);
            mService = null;
        } catch (RemoteException e) {
        }

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        try {
            mService = IService.Stub.asInterface(service);
            mService.registerCallback(mCallback.this);
        } catch (RemoteException e) {
        }
    }
};

public void bind() {
    if (!mServiceConnected) {
        mServiceConnected = true;
        Intent connectionIntent = new Intent(IService.class.getName());
        mActivity.bindService(connectionIntent, mServiceConnection,
                Context.BIND_AUTO_CREATE);

    } 
}
EN

回答 1

Stack Overflow用户

发布于 2015-06-01 06:12:40

试着用这个

代码语言:javascript
复制
 // inapp
    Intent serviceIntent = new Intent(
            "com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");

    bindService = bindService(serviceIntent, mServiceConn,
            Context.BIND_AUTO_CREATE);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24954724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档