尝试在我正在开发的应用程序中添加一些应用程序内购买,但事情不会这么顺利。
我有这样的零碎活动:

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

但由于某些原因,onServiceConnect回调从未发生。
有人知道是什么引起的吗?

最佳答案

我想是你解决的。不管怎么说,我也有同样的问题,我只是解决了它。
要使其正常工作,请删除此行:

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

07-26 07:27