如何在应用结算中隐藏或删除广告

时间:2013-08-26 23:17:14

标签: android in-app-purchase admob in-app-billing ads

我在应用结算中阅读了很多关于这个主题的文章,而nenashel没有关于如何隐藏或删除广告(admob)的实例。请帮忙。我需要一个有效的例子。 这是当下的代码

public class MainAct extends Activity {

    private static final String TAG = "com.sim.physics.inappbilling";
    IabHelper mHelper;
    static final String ITEM_SKU = "android.test.purchased";
    private Button clickButton;
    private Button buyButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repair);
        buyButton = (Button)findViewById(R.id.buyButton);
        clickButton = (Button)findViewById(R.id.clickButton);   
        clickButton.setEnabled(false);
        String base64EncodedPublicKey = "";

        mHelper = new IabHelper(this, base64EncodedPublicKey);

        mHelper.startSetup(new 
        IabHelper.OnIabSetupFinishedListener() {
              public void onIabSetupFinished(IabResult result) 
          {
                if (!result.isSuccess()) {
                   Log.d(TAG, "In-app Billing setup failed: " + 
                result);
              } else {             
                    Log.d(TAG, "In-app Billing is set up OK");
          }
           }
        });
} 
    public void buyClick(View view) {

        String payload = "";
         mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,   
               mPurchaseFinishedListener, payload);
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.example_list, menu);
        return true;
    }
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
    = new IabHelper.OnIabPurchaseFinishedListener() {

        public void onIabPurchaseFinished(IabResult result, 
                    Purchase purchase) 
    {
       if (result.isFailure()) {
          // Handle error
          return;
     }      
     else if (purchase.getSku().equals(ITEM_SKU)) {
         consumeItem();
        buyButton.setEnabled(false);
    }

   }
};
    public void buttonClicked (View view)
    {
        clickButton.setEnabled(false);
        buyButton.setEnabled(true);
    }
    protected void consumeItem() {
        mHelper.queryInventoryAsync(mReceivedInventoryListener);
    }

    IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener 
       = new IabHelper.QueryInventoryFinishedListener() {
           public void onQueryInventoryFinished(IabResult result,
              Inventory inventory) {


              if (result.isFailure()) {
              // Handle failure
              } else {
                     mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), 
                mConsumeFinishedListener);
              }
        }
    };

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
              new IabHelper.OnConsumeFinishedListener() {
               public void onConsumeFinished(Purchase purchase, 
                     IabResult result) {

             if (result.isSuccess()) {               
                  clickButton.setEnabled(true);
             } else {
                     // handle error
             }
          }
        };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, 
         Intent data) 
    {
          if (!mHelper.handleActivityResult(requestCode, 
                  resultCode, data)) {     
            super.onActivityResult(requestCode, resultCode, data);
          }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHelper != null) mHelper.dispose();
        mHelper = null;
    }`enter code here`
}

我可以添加什么以及在哪里隐藏和删除广告

我的广告布局文件          

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ListView>
    </LinearLayout>

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admob_publisher_id"
        ads:loadAdOnCreate="true"
        android:gravity="center_horizontal" >
    </com.google.ads.AdView>

</LinearLayout>

1 个答案:

答案 0 :(得分:3)

如果购买成功,在SharedPreferences中存储布尔值怎么样?然后,您可以检索该值,并在必要时显示或隐藏广告。

你的onCreate里面有这样的东西:

View ads = (AdView) findViewById(R.id.adView);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("purchased", false)) ads.setVisibility(View.GONE);

然后在你的onIabPurchaseFinished上:

else if (purchase.getSku().equals(ITEM_SKU)) {
        buyButton.setEnabled(false);
        ads.setVisibility(View.GONE);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("purchased", true);
        consumeItem();
    }