Java 如何在android中在后台运行时暂停应用程序?
这是我的代码,我想在后台运行时暂停加载应用程序,我在spice uno m-498 mobile中执行了我的代码。即使应用程序在后台运行,也会加载广告。请帮帮我。我想每次在显示广告之前检查你的应用程序是在前台还是后台都可以解决你的问题。在下面的博客中,他们详细解释了检测这些状态的方法Java 如何在android中在后台运行时暂停应用程序?,java,android,android-layout,android-intent,android-activity,Java,Android,Android Layout,Android Intent,Android Activity,这是我的代码,我想在后台运行时暂停加载应用程序,我在spice uno m-498 mobile中执行了我的代码。即使应用程序在后台运行,也会加载广告。请帮帮我。我想每次在显示广告之前检查你的应用程序是在前台还是后台都可以解决你的问题。在下面的博客中,他们详细解释了检测这些状态的方法 您可以尝试进行类似于if(YourActivity.this.isVisible())的检查,然后只显示添加。我没有为特定活动设置此选项。对于整个应用程序,中间广告仅从这里开始。所以我不能用这个密码。 //
您可以尝试进行类似于
if(YourActivity.this.isVisible())
的检查,然后只显示添加。我没有为特定活动设置此选项。对于整个应用程序,中间广告仅从这里开始。所以我不能用这个密码。
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(SplashActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(AD_UNIT_ID);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
}
private void displayInterstitial() {
// TODO Auto-generated method stub
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});
//Declare the timer
adTimer = new Timer();
//Set the schedule function and rate
adTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//Called each time when 1000 milliseconds (1 second) (the period parameter)
//We must use this function in order to change the text view text
runOnUiThread(new Runnable() {
@Override
public void run() {
//if(!isFirstLoad)
//{
show_ads(typeofAd);
//}
//isFirstLoad = false;
}
private void show_ads(int ad_val) {
// TODO Auto-generated method stub
System.out.println(ad_val);
Log.i("Ad type"," " + ad_val);
switch (ad_val) {
case 1:
interstitial.loadAd(new com.google.android.gms.ads.AdRequest.Builder().build());
break;
case 2:
interstitial.loadAd(new com.google.android.gms.ads.AdRequest.Builder().build());
break;
default:
break;
}
}
});
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
1000*60*1, //2 mins
//Set the amount of time between each execution (in milliseconds)
1000*60*2); //8 minutes
public void onDestroy()
{
super.onDestroy();
/*
* Kill application when the root activity is killed.
*/
// killApp(true);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onPause() {
super.onPause();
}// end method onPause
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
public void onBackPressed() {
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
//markerMyLocation = null;
//myLocationCircle = null;
}