如何在不破坏活动的情况下关闭活动?

时间:2014-02-02 11:10:51

标签: android android-activity ads interstitial

好的,首先,我有3个按钮的菜单,导致3种不同的布局。 我想要什么,我想在关闭我的应用程序时显示插页式广告。 会发生什么,为了节省内存,我必须在每个按钮上添加finish(),这样就不会占用所有内存。当我点击任何按钮插页式广告时。

问题是什么,我不能将我的代码设置为仅在我完全关闭app而不是在切换活动时才显示插页式广告 我的代码:

package puske.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.google.ads.*;
import com.google.ads.AdRequest.ErrorCode;

public class Menu extends Activity implements AdListener {
private InterstitialAd interstitial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    interstitial = new InterstitialAd(this, "ID");

    // Create ad request
    AdRequest adRequest = new AdRequest();

    // Begin loading your interstitial
    interstitial.loadAd(adRequest);

    // Set Ad Listener to use the callbacks below
    interstitial.setAdListener(this);
  }

  @Override
  public void onReceiveAd(Ad ad) {
{



Button button1=(Button)findViewById(R.id.menu1);
button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        startActivity(new Intent(Menu.this, Rifles.class));
        finish();

    }
});
Button button2=(Button)findViewById(R.id.menu2);
button2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        startActivity(new Intent(Menu.this, Pistols.class));
        finish();
    }

});



Button button4=(Button)findViewById(R.id.menu3);
button4.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        startActivity(new Intent(Menu.this, Snipers.class));
        finish();
        }
});
}
  }


  @Override
        protected void onDestroy() {
            interstitial.show();
            super.onDestroy();
             // explicitly release media player
            unbindDrawables(findViewById(R.id.menuzor));
            System.gc();
  }

    private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }

        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            try
            {
                ((ViewGroup) view).removeAllViews();
            }
            catch(UnsupportedOperationException ignore)
            {
            }
}
}



    @Override
    public void onLeaveApplication(Ad arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onDismissScreen(Ad arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPresentScreen(Ad arg0) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:1)

看看这张照片 enter image description here

onPause或onStop(); 试试吧