2

我已经阅读了许多关于应用计费主题的文章,并且没有关于如何隐藏或删除广告 (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>
4

1 回答 1

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();
    }
于 2013-08-27T02:56:19.130 回答