首页 > 解决方案 > 在 BillingProcessor.purchase 后应用计费不显示弹出消息

问题描述

我对 Android 中的应用内计费仍然不熟悉。我已经尝试过与本教程完全相同的方法,但从未弹出。这是链接

GitHub

教程视频

这是我的代码

主要活动

public class MainActivity extends AppCompatActivity implements BillingProcessor.IBillingHandler {
    BillingProcessor bp;
    Button purchase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        bp = new BillingProcessor(this, null, this);
        purchase = (Button) findViewById(R.id.purchase);

        purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bp.purchase(MainActivity.this, "android.test.purchased");
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bp.purchase(MainActivity.this, "android.test.purchased");
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
        showToast("You Have Purchased The Product");
    }

    @Override
    public void onPurchaseHistoryRestored() {

    }

    @Override
    public void onBillingError(int errorCode, @Nullable Throwable error) {
        showToast("An Error Has Occurred");
    }

    @Override
    public void onBillingInitialized() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(!bp.handleActivityResult(requestCode, resultCode, data)){
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

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

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
}

我已经在build.gradle APP中添加了这个

compile 'com.anjlab.android.iab.v3:library:1.0.+'

AndroidManifest中,我也添加了计费权限

<uses-permission android:name="com.android.vending.BILLING" />

即使我尝试与教程完全相同,我也真的被困住了。你能帮忙看看问题出在哪里。

标签: androidin-app-billing

解决方案


在最新版本的 google pay 中,许可证密钥和产品 ID不能再为空。您必须使用您的产品 ID 和开发人员许可证密钥创建一个 alpha 版本,就像它们在控制台中的方式一样。

此外,您必须在发布 alpha 版本后等待大约30 分钟才能测试您的计费。

希望有帮助!!

(确保您已添加 INTERNET 权限;将 build.gradle 中的“+”号替换为您将在更改日志中找到的版本)


推荐阅读