[ Google suspended my app for ads in background ]
yesterday google suspended my app from google store saying that my ads were against google TOS. Basically there is a sort of bug, my fault obviously and in some devices the advertises keep showing also if you press home button and let the app in background. I really don't know how to fix it.
This is xml code
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="INTERSTITIAL"
ads:adUnitId="MyID" >
</com.google.android.gms.ads.AdView>
this is the main
public class MainActivity extends ActionBarActivity
{
private WebView webView;
private InterstitialAd interstitial;
String Url="Myurl";
List<String> segments = new ArrayList<String>();
int delay = 120000; //milliseconds
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run()
{
//advertise();
handler.postDelayed(this, delay);
}
}, 7000);
//
}
public void advertise()
{
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("MyID");
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView) this.findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("CC5F2C72DF2B356BBF0DA198")
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener()
{
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}
public void displayInterstitial()
{
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded())
{
interstitial.show();
}
}*/
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.twitter:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/salvoaranzulla"));
startActivity(browserIntent);
break;
case R.id.facebook:
Intent browserIntent1 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/salvoaranzulla?ref=ts&fref=ts"));
startActivity(browserIntent1);
break;
}
return true;
}
}
Answer 1
Make your handler and runnable field variables and start and stop them like so.
@Override
public void onStart() {
super.onStart();
mHandler.postDelayed(mUpdateTimeTask, delay);
}
@Override
public void onStop() {
super.onStop();
mHandler.removeCallbacks(mUpdateTimeTask);
}
Answer 2
If this problem is not happening in every phone it could possibly the problem is in that user's mobile settings. Just check whether in his/her " settings > Developer Options > Do not keep activities " selected or not. If it is selected then the app will destroy every activities as soon as the user left the current activity. So when an interstitial is displayed , the current activity will be normally in onPause. but it will be destroyed and when the user close the interstitial and come back again oncreate will run and the ad will display.
If you want to control this things when the activity is leaving focus, it belongs in onPause(), onStop() or onDestroy() depending on the level of control you need.
Answer 3
Your app was probably banned because of this:
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener()
{
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
Do NOT display an ad from onAdLoaded()
, display it at a natural break point in your app.