Android 如何使用应用内计费隐藏或删除广告

Android 如何使用应用内计费隐藏或删除广告,android,in-app-purchase,admob,in-app-billing,ads,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 = "andr

我读过很多关于应用程序内计费的文章,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>
我可以在哪里添加什么来隐藏和删除广告

    <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>
我的广告布局文件

    <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>

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

    <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>
在onCreate中类似于以下内容:

    <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>
View ads = (AdView) findViewById(R.id.adView);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("purchased", false)) ads.setVisibility(View.GONE);
然后在您的购买完成后:

    <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>
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();
    }