简体   繁体   English

调用System.exit(0)后,Android应用程序未关闭

[英]Android application doesn't close after calling System.exit(0)

I have an Android app that worked great, before I've added admob activity. 在添加admob活动之前,我有一个运行良好的Android应用。 I'm closing my app with killing process (calling System.exit(0)). 我正在使用终止进程关闭我的应用程序(调用System.exit(0))。 I know that this is the worst solution of finishing the app. 我知道这是完成应用程序的最糟糕的解决方案。 I'm working with OpenGL states and libgdx framefork, so I can't fixed all memory leak that appear when I'm calling standard android finish() function. 我正在使用OpenGL状态和libgdx framefork,因此我无法修复在调用标准android finish()函数时出现的所有内存泄漏。

So here's the problem: 所以这是问题所在:

My app works normally several times. 我的应用正常运行了好几次。 I close and start it again and again. 我关闭并一次又一次地启动它。 All works fine, but suddenly admob view doesn't appear and when I'm trying to close, it freezes. 一切正常,但突然没有出现admob视图,当我尝试关闭时,它冻结了。 The sound works, last screen shows itself, but touching not working. 声音正常,最后一个屏幕显示了自身,但触摸不起作用。

When I'm killing the process by task manager, the music still playing. 当我用任务管理器终止进程时,音乐仍在播放。 Even when I completely remove the app, music still playing, so activity still working. 即使我完全删除了该应用程序,音乐仍在播放,因此活动仍在进行。 It stops only when I reboot my phone. 仅当我重新启动手机时它才会停止。

Without admob all works fine. 没有admob,一切正常。 I also trying destroy adView before closing, without result. 我也尝试在关闭前销毁adView,但没有结果。

Here is my main activity: 这是我的主要活动:

    public class ControllerActivity extends AndroidApplication{
    private  AdView adView;
    RelativeLayout layout;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = true;
        cfg.useCompass = false;
        cfg.useAccelerometer = false;

        layout = new RelativeLayout(this);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

        View gameView = initializeForView(Controller.getInstance(), cfg);

        adView = new AdView(this, AdSize.BANNER, "MYID"); 

        AdRequest adRequest=new AdRequest();
        adView.loadAd(adRequest);

        layout.addView(gameView);

        RelativeLayout.LayoutParams adParams = 
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                                RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        layout.addView(adView, adParams);

        setContentView(layout);
     }

        @Override
        public void onDestroy() {
        if (adView!=null) {
      adView.stopLoading();
          adView.destroy();
     }
        System.exit(0);
        super.onDestroy();
     }
 }

Have your any ideas, how to completely kill this process? 您有什么想法,如何彻底杀死这个过程?

I have found out some facts. 我发现了一些事实。 If the application is terminated by System.exit(0) or by android.os.Process.killProcess the next time AdMob won't show ads. 如果应用程序被System.exit(0)android.os.Process.killProcess终止,则下次AdMob将不会展示广告。 Even more if you try to terminate application it will stuck (the process remain active and the only option to kill it is the device reboot). 如果您尝试终止应用程序,则它甚至会继续阻塞(进程保持活动状态,并且杀死它的唯一选择是重新启动设备)。 The only solution is not to use System.exit(0) to exit application. 唯一的解决方案是不使用System.exit(0)退出应用程序。 It should be mentioned that it does not matter whether adView.destroy() or adView.stopLoading() was called. 应该提到的是, adView.destroy()还是adView.stopLoading()都没有关系。

I used this.moveTaskToBack(true); 我用this.moveTaskToBack(true); instead of termination. 而不是终止。 It will hide the application and if in some time it won't get restored Android will release all resources and AdMob will work OK. 它将隐藏应用程序,如果在一段时间后无法恢复,Android将释放所有资源,AdMob将正常运行。 If the application will be restored it will continue from the same place. 如果将还原该应用程序,它将从同一位置继续。

I continued testing and it seems that this is bug in the AdMob SDK 6.4.1. 我继续进行测试,看来这是AdMob SDK 6.4.1中的错误。 I have downloaded 6.3.0 and this bug has gone away. 我已经下载了6.3.0,并且这个错误已经消失了。

I usually use: 我通常使用:

android.os.Process.killProcess(android.os.Process.myPid()) android.os.Process.killProcess(android.os.Process.myPid())

to kill the process and all activities that were started. 终止进程和所有已开始的活动。

This is for a class that extends an Activity. 这是用于扩展Activity的类。 I'm not totally sure if it differs from extending AndroidApplication 我不太确定它是否不同于扩展AndroidApplication

Refer this i think it will work. 引用此我认为它将工作。

  <com.google.ads.AdView
             android:id="@+id/adView"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
             ads:adSize="SMART_BANNER"
             ads:adUnitId="@string/admobid"
             ads:loadAdOnCreate="true"
             ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
             android:background="@android:color/transparent" />

Or 要么

You can call adView.stopLoading() on activity destroy. 您可以在活动销毁时调用adView.stopLoading()

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

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