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

时间:2014-11-17 07:05:45

标签: java android android-layout android-intent android-activity

    // 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中执行了我的代码。即使应用在后台运行,它也会加载广告。请帮帮我..

1 个答案:

答案 0 :(得分:1)

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

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