2015-06-24 73 views
0

我試圖實施應用內結算API,並在用戶購買應用內購買的「刪除廣告」時讓我的應用沒有廣告。問題是我不知道該在哪裏做。我有一個FAB按鈕和廣告內的linierlayout像這樣: enter image description here用戶在應用內購買時刪除廣告

並且只要用戶購買它,我將設置廣告的可見性,以便FAB按鈕關閉。

這是我的主要活動代碼:

public class MainActivity extends AppCompatActivity { 
private static final String TAG = MainActivity.class.getSimpleName(); 
private Toolbar mToolbar; 
private RecyclerView mRecyclerView; 
private ArrayList<String> shoppingListItems; 
private SharedPreferences mSharedPreferences; 
private SharedPreferences.Editor mEditor; 
private TextView mEmptyTextView; 
private ShoppingListAdapter adapter; 
private ActionButton actionButton; 
private MaterialDialog addItemdialog = null; 
private IabHelper mHelper; 
private String SKU_REMOVE_ADDS = "remove_adds_sku"; 
private boolean mIsRemoveAdds = false; 
private IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
    @Override 
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
     if (result.isFailure()) { 
      Log.d(TAG, "Error purchasing: " + result); 
      return; 
     } 
     else if (purchase.getSku().equals(SKU_REMOVE_ADDS)) { 
      // consume the gas and update the UI 
      mIsRemoveAdds = true; 
      Toast.makeText(MainActivity.this,"Purchase successful",Toast.LENGTH_LONG).show(); 
     } 
    } 
}; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //load ads 
    AdView mAdView = (AdView) findViewById(R.id.adView); 
    AdRequest adRequest = new AdRequest.Builder().build(); 
    mAdView.loadAd(adRequest); 
//set up billing 
    String publicKey = "PUBLIC_KEY"; 

    mHelper = new IabHelper(this,publicKey); 
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
     @Override 
     public void onIabSetupFinished(IabResult result) { 
      if(!result.isSuccess()){ 
       //error 
       Log.d(TAG,"Proglem setting up in-app Billing: " + result); 
      } 

      //Horay, IAB is fully set up! 
      Log.d(TAG,"Horay, IAB is fully set up!"); 
     } 
    }); 
    queryPurchasedItems(); 
} 
@Override 
protected void onResume() { 
    super.onResume(); 
    isListEmpty(); 
    queryPurchasedItems(); 
} 
@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    if (mHelper != null) mHelper.dispose(); 
    mHelper = null; 
} 
private void queryPurchasedItems() { 
    //check if user has bought "remove adds" 
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 
     @Override 
     public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
      if (result.isFailure()) { 
       // handle error here 
      } 
      else{ 
       // does the user have the premium upgrade? 
       mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS); 
       // update UI accordingly 
      } 
     } 
    }; 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
if(id == R.id.action_remove_adds){ 
     mHelper.launchPurchaseFlow(this,SKU_REMOVE_ADDS,1,mPurchasedFinishedListener,""); 
    } 
return super.onOptionsItemSelected(item); 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data); 

    // Pass on the activity result to the helper for handling 
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) { 
     // not handled, so handle it ourselves (here's where you'd 
     // perform any handling of activity results not related to in-app 
     // billing... 
     //setContentView(R.layout.activity_main_adds); 
     super.onActivityResult(requestCode, resultCode, data); 
    } 
    else { 
     Log.d(TAG, "onActivityResult handled by IABUtil."); 
     //setContentView(R.layout.activity_main_adds); 
    } 
} 
+0

是的你可以這樣做@Georgi Koemdzhiev –

+0

我知道我可以做到這一點,但由於我第一次實施應用內結算API,所以這有點令人困惑,所以我在這裏尋求人們的幫助誰知道比我知道的方式:) –

回答

1

是的,你的解決方案適合於那種情景。事實上,我對my app使用了類似的方法,對於多個IAP事務,它工作正常。

雖然有一個建議。以防萬一,您應該在您的SharedPreferences中堅持mIsRemoveAdds。完成後,您可以在檢測到設備未連接到互聯網(飛行模式,或者可能只是錯誤的接收)時從SharedPreferences中檢索值。

除此之外,你的代碼是好的。

+0

謝謝你的建議。我正在考慮將mIsRemoveAdds存儲到SharedPreferences中,但是我認爲,查詢所購商品的庫存將採購以現金存儲,並且在沒有互聯網的情況下查詢它將會很好。 –

+1

是的,正如我所說:*以防萬一*。對於IAP,用戶是(正確)挑剔的。我是那種傾向於安全的人。 ;) – ridsatrio

+0

謝謝,如果購買成功,我已經遵循了您的建議並將值存儲在sharedPreferences中。再次感謝! :) –