如何在应用启动时验证用户是否购买了非消耗性产品

时间:2019-06-05 09:04:27

标签: java android in-app-purchase verify

我刚刚完成了使用Google Play计费和aidl设置inapp计费的操作。 成功购买后,高级功能将通过布尔值激活。但是,在应用程序关闭并重新启动后,高级功能便消失了。即布尔值恢复为false。我想知道如何确保只要购买了赠品,应用启动后布尔值就保持真实。

在MainActivity上

    public class MainActivity extends AppCompatActivity {

public static boolean proFeature = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

关于InAppBilling活动

    public class InAppBilling extends Activity implements IabBroadcastReceiver.IabBroadcastListener {
private static final String TAG = ".InAppBilling";

IabHelper mHelper;
boolean premiumFeature = false;
static final String SKU_PREMIUM = "android.test.purchased";
static final int RC_REQUEST = 10001;
IabBroadcastReceiver mBroadcastReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_lay);

}

    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    @Override
    public void onQueryInventoryFinished(IabResult result, Inventory inv) {
        Log.d(TAG, "Query inventory finished.");

        if (mHelper == null) return;

        if (result.isFailure()) {
            complain("Failed to query inventory: " + result);
            return;
        }

        Log.d(TAG, "Query inventory was successful.");

        Purchase premiumPurchase = inv.getPurchase(SKU_PREMIUM);
        premiumFeature = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
        Log.d(TAG, "User is " + (premiumFeature ? "PREMIUM" : "NOT PREMIUM"));

        updateUi();
        setWaitScreen(false);
        Log.d(TAG, "Initial inventory query finished; enabling main UI.");
    }
};

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    @Override
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);

        if (mHelper == null) return;

        if (result.isFailure()) {
            complain("Error purchasing: " + result);
            setWaitScreen(false);
            return;
        }
        if (!verifyDeveloperPayload(purchase)) {
            complain("Error purchasing. Authenticity verification failed.");
            setWaitScreen(false);
            return;
        }

        Log.d(TAG, "Purchase successful.");

        if (purchase.getSku().equals(SKU_PREMIUM)) {
            // bought the premium upgrade!
            Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
            alert("Thank you for upgrading to premium!");
            premiumFeature = true;
            updateUi();
            setWaitScreen(false);
        }

    }
};

    public void updateUi(){
    button.setVisibility(premiumFeature ? View.GONE : View.VISIBLE);
    if (premiumFeature){
        MainActivity.proFeature = true;
    }else{
    MainActivity.proFeature = false;
    }
}

2 个答案:

答案 0 :(得分:0)

您可以将其保存到共享首选项或服务器上,并在开始(启动屏幕)中查询状态。

或者您可以在初始屏幕中进行库存查询以检查初始屏幕中的状态。

答案 1 :(得分:0)

在您的购买完成监听器中,修改以下代码以将价值存储到共享的偏好中。

       if (purchase.getSku().equals(SKU_PREMIUM)) {
            // bought the premium upgrade!
            Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
            alert("Thank you for upgrading to premium!");
            premiumFeature = true;

            SharedPreferences sharedPref = context.getSharedPreferences(
                  "my_sp", Context.MODE_PRIVATE);
            sharedPref.edit().putBoolean("isPremium, premiumFeature).commit();

            updateUi();
            setWaitScreen(false);
        }

然后在应用重新启动时再次从共享首选项中获取该值。

SharedPreferences sharedPref = context.getSharedPreferences(
                      "my_sp", Context.MODE_PRIVATE);
premiumFeature = sharedPref.getBoolean("isPremium, false);