gpt4 book ai didi

java - 如何在应用程序启动时验证用户购买非消耗性产品

转载 作者:行者123 更新时间:2023-12-02 04:49:05 25 4
gpt4 key购买 nike

我刚刚使用 google play billing 和 aidl 完成了应用内结算设置。成功购买后,高级功能将通过 boolean 值激活。但在应用程序关闭并重新启动后,高级功能就会消失。即 boolean 值恢复为 false。我想知道只要购买了高级版,如何确保 boolean 值在应用程序启动后保持为 true。

在主要 Activity 上

    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 Activity

    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;
}
}

最佳答案

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

       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);

更新(2022 年 2 月 19 日):

  • 正如 @Shazniq 所说,为了安全起见,将这些详细信息以及用户个人资料数据存储在服务器上总是好的。因此,每次启动应用程序时,您都可以验证详细信息。您必须在需要时应用自己的逻辑来验证它。

关于java - 如何在应用程序启动时验证用户购买非消耗性产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56457372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com