在android中调用bindService

时间:2014-07-25 11:27:10

标签: android service

我在 MyFragment.class 中的bind()呼叫onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)onResume()我正在尝试访问 mSerive 但是它在onServiceConnected(ComponentName name, IBinder service)之后显示 NULL POINTER EXCEPTION onResume()来电。为什么在我调用bind()时没有调用onServiceConnected可以有人给出关于它的建议吗?

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);

    } 
}

1 个答案:

答案 0 :(得分:0)

试试这个

 // inapp
    Intent serviceIntent = new Intent(
            "com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");

    bindService = bindService(serviceIntent, mServiceConn,
            Context.BIND_AUTO_CREATE);