我試圖實施應用內結算API,並在用戶購買應用內購買的「刪除廣告」時讓我的應用沒有廣告。問題是我不知道該在哪裏做。我有一個FAB按鈕和廣告內的linierlayout像這樣: 用戶在應用內購買時刪除廣告
並且只要用戶購買它,我將設置廣告的可見性,以便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);
}
}
是的你可以這樣做@Georgi Koemdzhiev –
我知道我可以做到這一點,但由於我第一次實施應用內結算API,所以這有點令人困惑,所以我在這裏尋求人們的幫助誰知道比我知道的方式:) –