在应用程序计费需要钱,但不会给产品

时间:2012-11-17 14:56:40

标签: android in-app-billing

当我拨打购买屏幕时,我可以购买应用程序内的产品,一切正常,但当用户离开应用程序并返回查看产品时,它一直要求他们再次购买,

我需要知道如何做到这一点所以当用户购买应用程序商店屏幕不再出现并且他们可以访问产品时,应用程序的设计使得当用户购买应用程序时他们可以访问内置功能的新活动

如果有人可以提供帮助,我将非常感激

我使用了本教程,这对我开始非常有帮助:[TUT] Simple InApp Billing / Payment By blundell

这是我的代码

      package com.IrishSign.app;

    import java.util.Locale;

    import com.IrishSign.app.BillingHelper;
    import com.IrishSign.app.R;
    import com.IrishSign.app.BillingService;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;

    public class IrishSignAppActivity extends Activity implements OnClickListener {
        private static final String TAG = "BillingService";

        private Context mContext;
        private ImageView purchaseableItem;
        private Button purchaseButton;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.i("BillingService", "Starting");
        setContentView(R.layout.main);
        mContext = this;
        Button A = (Button) findViewById(R.id.alphabet);
        Button purchaseableItem = (Button) findViewById(R.id.topics);
        Button Intro = (Button) findViewById(R.id.intro);
        Button G = (Button) findViewById(R.id.about);

        purchaseableItem.setOnClickListener(this);
        startService(new Intent(mContext, BillingService.class));
        BillingHelper.setCompletedHandler(mTransactionHandler);

        A.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent1 = new Intent("com.IrishSign.app.alpha");
                startActivity(intent1);
            }
        });

        Intro.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent1 = new Intent("com.IrishSign.app.Intro");
                startActivity(intent1);
            }
        });

        G.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog alertDialog = new AlertDialog.Builder(
                        IrishSignAppActivity.this).setCancelable(false)
                        .create(); // Reads Update
                alertDialog.setTitle("Welcome");
                alertDialog.setMessage("-----");// 

                alertDialog.setButton("Continue",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int arg1) {
                                Intent intent5 = new Intent(
                                        IrishSignAppActivity.this,
                                        IrishSignAppActivity.class);

                            }
                        });

                alertDialog.show(); // <-- Shows dialog on screen.
            }

        });

    }

    public Handler mTransactionHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            Log.i(TAG, "Transaction complete");
            Log.i(TAG, "Transaction status: "
                    + BillingHelper.latestPurchase.purchaseState);
            Log.i(TAG, "Item purchased is: "
                    + BillingHelper.latestPurchase.productId);

            if (BillingHelper.latestPurchase.isPurchased()) {
                Intent intent = new Intent("com.IrishSign.app.Topics");
                startActivity(intent);
            }
        };

    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.topics:
            if (BillingHelper.isBillingSupported()) {
                BillingHelper.requestPurchase(mContext,
                        "com.blundell.item.passport");
                // android.test.purchased or android.test.canceled or
                // android.test.refunded or com.blundell.item.passport
            } else {
                Log.i(TAG, "Can't purchase on this device");
                purchaseButton.setEnabled(false); // XXX press button before
                                                    // service started will
                                                    // disable when it shouldnt
            }

            break;
        default:
            // nada
            Log.i(TAG, "default. ID: " + v.getId());
            break;
        }

    }

    @Override
    protected void onPause() {
        Log.i(TAG, "onPause())");
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        BillingHelper.stopService();
        super.onDestroy();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用restoreTransactions检查应用程序的启动时间。如果您使用过托管产品或订阅,那么只有您将获得该用户的所有详细信息。

对于非托管产品,谷歌没有维护详细信息。

所以在你的主要活动中调用它

mBillingService = new BillingService();
mBillingService.setContext(this);
mBillingService.restoreTransactions();

在ResponseHandler类中调用此方法后,有一个方法purchaseResponse

purchaseResponse(final Context context,
        final PurchaseState purchaseState, final String productId,
        final String orderId, final long purchaseTime,
        final String developerPayload, final String purchaseToken) {
 }

将返回所有细节。

您可以在

之后检查purchaseState
       if (purchaseState == PurchaseState.PURCHASED) {

        } else if (purchaseState == PurchaseState.REFUNDED) {

        } else if (purchaseState == PurchaseState.CANCELED) {

        }