繁体   English   中英

如何在android中在后台运行时暂停应用程序?

[英]How to pause an app while running in the background in android?

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM