gpt4 book ai didi

java - Android 中如何暂停后台运行的应用程序?

转载 作者:行者123 更新时间:2023-12-01 12:16:28 25 4
gpt4 key购买 nike

    // 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;
}

这是我的代码,我想在后台运行时暂停加载应用程序,我在spice uno m-498 mobile中执行了我的代码。即使应用程序在后台运行,它也会加载广告。请帮助我..

最佳答案

我认为每次在展示广告之前检查您的应用程序是在前台还是后台将解决您的问题。在下面的博客中,他们详细解释了如何检测这些状态

http://steveliles.github.io/is_my_android_app_currently_foreground_or_background.html

关于java - Android 中如何暂停后台运行的应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26967281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com