Show how to remove ads

This commit is contained in:
M66B 2016-07-25 12:11:39 +02:00
parent 63d448c1a9
commit 131c945c64
3 changed files with 72 additions and 35 deletions

View File

@ -42,12 +42,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/allDebug/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/allDebug/renderscript" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/allDebug/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/androidTest/all/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/androidTest/all/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/androidTest/all/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/androidTest/all/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/androidTest/all/debug" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/resValues/androidTest/all/debug" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testAllDebug/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testAllDebug/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testAllDebug/assets" type="java-test-resource" />
@ -56,6 +50,12 @@
<sourceFolder url="file://$MODULE_DIR$/src/testAllDebug/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testAllDebug/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testAllDebug/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/androidTest/all/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/androidTest/all/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/androidTest/all/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/androidTest/all/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/androidTest/all/debug" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/resValues/androidTest/all/debug" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/all/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/all/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/all/assets" type="java-resource" />
@ -64,14 +64,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/all/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/all/renderscript" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/all/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testAll/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testAll/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testAll/assets" type="java-test-resource" />
@ -80,6 +72,14 @@
<sourceFolder url="file://$MODULE_DIR$/src/testAll/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testAll/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testAll/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestAll/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/assets" type="java-resource" />

View File

@ -33,6 +33,7 @@ import android.net.Uri;
import android.net.VpnService;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.NonNull;
@ -61,6 +62,7 @@ import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
@ -836,18 +838,24 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
}
private void loadAds() {
// https://developers.google.com/android/reference/com/google/android/gms/ads/package-summary
MobileAds.initialize(getApplicationContext(), getString(R.string.ad_app_id));
AdView adView = (AdView) findViewById(R.id.adView);
final TextView tvAd = (TextView) findViewById(R.id.tvAd);
final AdView adView = (AdView) findViewById(R.id.adView);
tvAd.setVisibility(View.VISIBLE);
adView.setVisibility(View.VISIBLE);
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.i(TAG, "Ad loaded");
tvAd.setVisibility(View.GONE);
}
@Override
public void onAdFailedToLoad(int errorCode) {
tvAd.setVisibility(View.VISIBLE);
switch (errorCode) {
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
Log.w(TAG, "Ad load error=INTERNAL_ERROR");
@ -882,17 +890,17 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
}
});
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(getString(R.string.ad_test_device_id))
.build();
adView.loadAd(adRequest);
requestAds(adView);
}
private void reloadAds() {
TextView tvAd = (TextView) findViewById(R.id.tvAd);
AdView adView = (AdView) findViewById(R.id.adView);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) adView.getLayoutParams();
LinearLayout parent = (LinearLayout) adView.getParent();
tvAd.setVisibility(View.VISIBLE);
adView.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) adView.getLayoutParams();
RelativeLayout parent = (RelativeLayout) adView.getParent();
parent.removeView(adView);
adView.destroy();
@ -903,19 +911,32 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
adView.setLayoutParams(params);
parent.addView(adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(getString(R.string.ad_test_device_id))
.build();
adView.loadAd(adRequest);
requestAds(adView);
}
private void requestAds(final AdView adView) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(getString(R.string.ad_test_device_id))
.build();
adView.loadAd(adRequest);
}
}, 1000);
}
private void disableAds() {
TextView tvAd = (TextView) findViewById(R.id.tvAd);
AdView adView = (AdView) findViewById(R.id.adView);
if (adView != null) {
LinearLayout parent = (LinearLayout) adView.getParent();
if (adView != null) {
RelativeLayout parent = (RelativeLayout) adView.getParent();
parent.removeView(tvAd);
parent.removeView(adView);
tvAd.setVisibility(View.GONE);
adView.destroy();
}
}

View File

@ -149,13 +149,29 @@
android:textAppearance="@style/TextSmall" />
</LinearLayout>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/ad_banner_unit_id" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvAd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/title_pro_ads"
android:textAppearance="@style/TextSmall"
android:textColor="?attr/colorOff"
android:textStyle="italic" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/ad_banner_unit_id" />
</RelativeLayout>
</LinearLayout>
<View