[英]Checking if in-app purchases are owned in Android on first launch
我試圖在首次啟動應用程序時嘗試檢測用戶是否擁有任何應用程序內購買商品,並嘗試使用SharedPreferences續訂應用程序的Pro模式。 以下代碼不幸地不起作用:(
if (version.equals("null")) { //checking version of the app, if it is unset equals first launch
SharedPreferences.Editor editor = appinfo.edit();
version = currentversion;
editor.putString("version", version);
editor.apply();
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (mHelper == null) return; //IabHelper mHelper;
Purchase purchase = Inventory.getPurchase("sku1");
Purchase purchase2 = Inventory.getPurchase("sku2");
Purchase purchase3 = Inventory.getPurchase("sku3");
if (purchase != null || purchase2 != null || purchase3 != null) {
final SharedPreferences ispro = getApplicationContext().getSharedPreferences("ispro", 0);
SharedPreferences.Editor editor = ispro.edit();
editor.putInt("ispro", 1);
editor.apply();
}
}
};
startActivity(new Intent(MainPage.this, Changelog.class));
EDIT1:經過一些編輯后,代碼現在如下所示:
final List<String> skus = Arrays.asList("sku1", "sku2", "sku3");
if (version.equals("null")) {
SharedPreferences.Editor editor = appinfo.edit();
version = currentversion;
editor.putString("version", version);
editor.apply();
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
}
if (mHelper == null) return;
mBroadcastReceiver = new IabBroadcastReceiver(MainPage.this);
IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION);
registerReceiver(mBroadcastReceiver, broadcastFilter);
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (mHelper == null) return;
Purchase purchase = Inventory.getPurchase("sku1");
Purchase purchase2 = Inventory.getPurchase("sku2");
Purchase purchase3 = Inventory.getPurchase("sku3");
if (purchase != null || purchase2 != null || purchase3 != null) {
final SharedPreferences ispro = getApplicationContext().getSharedPreferences("ispro", 0);
SharedPreferences.Editor editor = ispro.edit();
editor.putInt("ispro", 1);
editor.apply();
}
}
};
try {
mHelper.queryInventoryAsync(true, skus, null, mReceivedInventoryListener);
} catch (IabHelper.IabAsyncInProgressException e) {
}
}
});
startActivity(new Intent(MainPage.this, Changelog.class));
我不知道這段代碼有什么問題。 預先感謝您的幫助和新年快樂! :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.