Android的应用内结算,如何处理应用程序的重新安装 [英] Android in-app billing, how to handle app reinstalls

查看:341
本文介绍了Android的应用内结算,如何处理应用程序的重新安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何应用内结算工作非常困惑。我读过的文档,我必须错过了一些东西,因为我不明白的最后一步,我需要落实到我的应用程序,使这项工作。该应用内结算的伟大工程,但是,如果一个用户卸载我的应用程序,并重新安装它在未来某一日期,我的应用程序不知道如何确定应用程序内购买了previously而制定的。下面是我的主类,我试图处理所有的这一个片段:

  @覆盖
公共无效的onCreate(包savedInstanceState)
{
    mContext =这一点;
    startService(新意图(mContext,BillingService.class));
    BillingHelper.setCompletedHandler(mTransactionHandler);
}
 

我使用的示例类从地牢示例项目。我不明白的是如何跌破code ++工程,在购买的时候,但重新运行它不工作,检查,如果事情已经被购买。我一直停留在这部分约一个月,现在,我已经变得非常沮丧吧。

 公开处理程序mTransactionHandler =新的处理程序()
{
    公共无效的handleMessage(android.os.Message MSG)
    {
        Log.i(TAG,交易完成);
        Log.i(TAG,交易状态
            + BillingHelper.latestPurchase.purchaseState);
        Log.i(TAG,购买的物品是:
            + BillingHelper.latestPurchase.productId);

        如果(BillingHelper.latestPurchase.isPurchased())
        {
            新的消息(谢谢!,getApplicationContext());
            PAY_VERSION = TRUE;
            共享preferences购买preferences = getShared preferences(PURCHASE_ preFERENCES,Activity.MODE_PRIVATE);
            编辑purchaseEditor =购买preferences.edit();
            purchaseEditor.putBoolean(购买,PAY_VERSION);
            purchaseEditor.commit();
            意向意图= getIntent();
            完();
            startActivity(意向);
        }
    };
 

我需要的是一些方法来查询服务器,看看这个项目已被购买或没有。据我所知,有一个 PURCHASE_STATE_CHANGED 的事情有没有什么地方,但是我不知道如何做任何事情,当它决定了状态已更改或如何启动它来检查。我迷路了,所有我需要的是一个很好的推动方向是正确的,因为到目前为止,我只是完全丧失。

编辑:

我还听说,你需要解析JSON,但我不知道如何甚至开始这样做。

编辑2:

我应该把这个东西来检查?

  BillingHelper.restoreTransactionInformation(BillingSecurity.generateNonce());
BillingHelper.getPurchaseInformation(新的String [] {myItem});
 

这code previously已经坠毁在我的姐妹们的电话(SGS3 , ICS <$ C C $> ),但不是我的( GN ICS JB 工作)。我称其为的onCreate()我的第一个活动。真的不知道该怎么办 getPurchaseInformation(...)一旦它被调用。它没有返回值,所以我不知道我是否可以解析 JSON 或任何我应该做的......

此外,那些2线给我这样的:

 一十一月8日至27号:54:04.271:E / BillingService有(17702):BillingHelper不完全实例化
11月8号至27号:54:04.271:E / BillingService有(17702):BillingHelper不完全实例化
 

解决方案

您是在正确的轨道上的第二次更新。关键是要认识到一切都在异步完成的。

典型的流程如下:

  1. 用户安装的应用程序。

  2. 在你的应用程序的第一次加载,你检查,如果你需要恢复 购买。

  3. 如果你这样做,发送RESTORE_TRANSACTION同步请求给谷歌。

  4. 谷歌将与acknowlegment响应响应您的 RESTORE_TRANSACTION请求。 (这是只是一个acknowlegement 的     他们收到了您的请求。)

  5. 在这一点上,你应该标记你已经派出一个恢复请求,谷歌并没有进一步的恢复需要从应用程序发送的。

  6. 现在的异步谷歌将开始发送PURCHASE_STATE_CHANGED事件,您的应用程序的每个应用程序内购买的用户拥有previously购买。此调用是在作为什么谷歌将派出如果用户已经做出了购买的第一次。

  7. 由于它是同一个电话,您的应用程序便拿起事件并处理它通常,如果用户刚刚购买应用程序内的产品(从而恢复购功能)。

在考虑到步骤2和5,我已经为我的应用程序做的是保持了一个名为共享preference值'APP_INITIALISED,默认为false。每次我的应用程序启动时,如果'APP_INITIALISED'是假的,我告诉谷歌要RESTORE_TRANSACTION(步骤2)然后我设定APP_INITIALISED为真(步骤5)。

I'm very confused on how in-app billing works. I've read the documentation and I must have missed something because I don't understand the final step I need to implement into my application to make this work. The in-app billing works great, however, if a user uninstalls my app and installs it again at a future date, my application doesn't know how to determine if the in-app purchase has previously been made. Here's a snippet from my main class where I attempt to handle all of this:

@Override
public void onCreate(Bundle savedInstanceState)
{
    mContext = this;
    startService(new Intent(mContext, BillingService.class));
    BillingHelper.setCompletedHandler(mTransactionHandler);
}

I am using the example classes from the dungeons example project. What I don't understand is how the below code works at the time of purchase, but re-running it doesn't work to check if something been purchased already. I have been stuck on this part for about a month now and I've been getting very frustrated with it.

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())
        {
            new Message("Thank you!", getApplicationContext());
            PAY_VERSION = true;
            SharedPreferences purchasePreferences = getSharedPreferences(PURCHASE_PREFERENCES, Activity.MODE_PRIVATE);
            Editor purchaseEditor = purchasePreferences.edit();
            purchaseEditor.putBoolean("purchased", PAY_VERSION);
            purchaseEditor.commit();
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }
    };

What I need is some way to query the server to see if this item has been purchased or not. I understand that there's a PURCHASE_STATE_CHANGED thing there somewhere however I have no idea how to do anything when it determines the state has changed or how to initiate it to check. I'm lost and all I need is a good push in the right direction because so far I'm just completely lost.

EDIT:

I've also heard you need to parse JSON, but I have no idea how to even begin doing that.

EDIT 2:

Am I supposed to call this stuff to check?

BillingHelper.restoreTransactionInformation(BillingSecurity.generateNonce());
BillingHelper.getPurchaseInformation(new String[] {"myItem"});

That code previously had crashed on my sisters phone (SGS3, ICS) but not on mine (GN, ICS, and JB work). I was calling it in onCreate() of my first activity. Not really sure what to do with getPurchaseInformation(...) once it's been called. It has no return value so I'm not sure if I can parse the JSON or whatever I'm supposed to do...

Also, those 2 lines give me this:

08-27 11:54:04.271: E/BillingService(17702): BillingHelper not fully instantiated
08-27 11:54:04.271: E/BillingService(17702): BillingHelper not fully instantiated

解决方案

You are on the right track with the second update. The trick is to realise everything is done in async.

The typical flow is as follows:

  1. User installs your app.

  2. On first load of your app, you check if you need to restore purchases.

  3. If you do, send a RESTORE_TRANSACTION synchronous request to Google.

  4. Google will respond with a acknowlegment response to your RESTORE_TRANSACTION request. (This is only an acknowlegement that they received your request.)

  5. At this point, you should mark that you had already sent a restore request to Google and no further restores needs to be sent from the app.

  6. Now asynchronously Google will start sending a 'PURCHASE_STATE_CHANGED' event to your app for each in-app purchase the user has previously purchased. This call is the same as what Google would had sent if the user had made that purchase for the first time.

  7. Since it's the same call, your app would pick up the event and handled it normally as if the user has just purchased the in-app product (thereby "restoring" the purchased feature).

In regard to steps 2 and 5, what I've done for my app is to keep a SharedPreference value called 'APP_INITIALISED' that defaults to false. Everytime my app starts up, if 'APP_INITIALISED' is false, I tell Google to RESTORE_TRANSACTION (step 2) then I set APP_INITIALISED to true (step 5).

这篇关于Android的应用内结算,如何处理应用程序的重新安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆