Android IAB:设备上无法使用计费服务

2024-05-01

我正在尝试在我的应用程序中实现 IAB。每次应用程序启动时,启动都会失败并显示:

Problem setting up In-app Billing: IabResult: Billing service unavailable on device. (response: 3:Billing Unavailable)

我已经尝试了几乎所有的方法:

  • 确保我有com.android.vending.BILLING清单中的许可。
  • 将两个版本对应的 APK 上传到 beta 通道。
  • 使用测试帐户。
  • 清除 Google Play 缓存。
  • 确保我的设备中有最新的 Google Play 服务build.gradle.
  • 仔细检查公共编码密钥。
  • 使用多个设备。

但无论如何,错误仍然存​​在。我已经阅读了所有有关它的问题和答案,但我不知道我错过了什么!

这是我的相关代码:

 public class MainActivity extends AppCompatActivity {

    IabHelper       mHelper; 

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHelper = new IabHelper(this, Billing.ENCODED_PUBLIC_KEY);
        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                if (!result.isSuccess()) {
                    // Oh noes, there was a problem.
                    Log.d("LOG", "Problem setting up In-app Billing: " + result);
                }
                // Hooray, IAB is fully set up!
            }
        });
        final SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        IabHelper.QueryInventoryFinishedListener mGotInventoryListener
                = new IabHelper.QueryInventoryFinishedListener() {
            public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
                if (result.isFailure()) {
                    SettingsActivity.premium = saved_values.getBoolean("premium",false);
                }
                else {
                    // does the user have the premium upgrade?
                    SettingsActivity.premium = inventory.hasPurchase(Billing.SKU_PREMIUM);
                    // update UI accordingly
                }
            }
        };
        try{
            mHelper.queryInventoryAsync(mGotInventoryListener);
        }
        catch (Exception e){
            SettingsActivity.premium = saved_values.getBoolean("premium",false);
        }
    }



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



    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("LOG", "onActivityResult(" + requestCode + "," + resultCode + "," + data);

        // Pass on the activity result to the helper for handling
        if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        }
        else {
            Log.i("LOG", "onActivityResult handled by IABUtil.");
        }
    }

这是控制台输出:

03-11 20:35:27.896 710-710/? W/ActivityManager: Permission Denial: getCurrentUser() from pid=14441, uid=10189 requires android.permission.INTERACT_ACROSS_USERS
03-11 20:35:28.516 14441-14441/com.johan.app D/LOG: Problem setting up In-app Billing: IabResult: Billing service unavailable on device. (response: 3:Billing Unavailable)
03-11 20:35:28.516 14441-14441/com.johan.app E/IabHelper: In-app billing error: Illegal state for operation (queryInventory): IAB helper is not set up.

请注意,我有代码在其他地方查询库存,但事实上它并没有超出mHelper.startUpSetup()使它变得无关紧要。


尝试这个!前往IabHelper类,在startSetup下的方法onServiceConnected回调你可以看到Intent.只需替换为以下代码即可。 (或者只是查找BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE).

Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
    // service available to handle that Intent
    mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
} else {
    // no service available to handle that Intent
    if (listener != null) {
        listener.onIabSetupFinished(
        new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE, "Billing service unavailable on device."));
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android IAB:设备上无法使用计费服务 的相关文章

随机推荐