3

尝试将一些应用内购买添加到我正在开发的应用中,但事情不会那么顺利。

我有这样的 FragmentActivity:

public class TestInAppBilling extends FragmentActivity{

//Application context reference
private static Context context;

/*
Billing stuff
 */
private IInAppBillingService mService;
private ServiceConnection mServiceConn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_layout);

    context = getApplicationContext();

    if(mServiceConn == null){
        mServiceConn = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName name) {
                mService = null;
            }

            @Override
            public void onServiceConnected(ComponentName name,
                                           IBinder service) {
                mService = IInAppBillingService.Stub.asInterface(service);
                System.out.println("Bound!");
            }
        };

        context.bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mServiceConn != null) {
        unbindService(mServiceConn);
    }
}
}

但由于某种原因,onServiceConnected 回调永远不会发生。

有谁知道是什么原因造成的?

4

1 回答 1

1

我认为你解决了它。无论如何,我遇到了同样的问题,我只是修复了它。要使其正常工作,请删除此行:

context.bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);

并添加:

setContentView(R.layout.test_layout);
context = getApplicationContext();
Intent intent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
intent.setPackage("com.android.vending");
getContext().bindService(intent, mServiceConn, getActivity().BIND_AUTO_CREATE);
于 2017-02-07T23:23:42.667 回答