从未调用过Android InAppBilling onServiceConnected

时间:2013-06-20 02:59:09

标签: android in-app-purchase in-app-billing

尝试将一些应用内购买添加到我正在处理的应用中,但事情并不顺利。

我有像这样的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回调永远不会发生。

任何人都知道导致它的原因吗?

1 个答案:

答案 0 :(得分: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);