简体   繁体   English

如何知道我何时关闭应用程序?

[英]How to know when I close my app?

My question is simple, I tried disable the bluetooth for my app when the user close the app. 我的问题很简单,当用户关闭应用程序时,我尝试为我的应用程序禁用蓝牙。 I need that if the user is with the app in background, the bluetooth maintain active, but if the user slide the app out of screen, the bluetooth closes. 我需要,如果用户将应用程序置于后台,则蓝牙保持活动状态,但是如果用户将应用程序滑出屏幕,则蓝牙将关闭。

I know how disable the bluetooth. 我知道如何禁用蓝牙。

if (mBluetoothAdapter.isEnabled()){
    mBluetoothAdapter.disable();
}

Works perfectly, but I need call that 3 lines when the user close the app, I tried with onDestroy , onPause , onStop , but noone works for me. 效果很好,但是当用户关闭应用程序时,我需要调用3行,我尝试使用onDestroyonPauseonStop ,但是没有人适合我。

onDestroy method is called when the user touch back button and the task go for background. 当用户触摸后退按钮并且任务进入后台时,将调用onDestroy方法。 onPause method is called when the user touch the recents button, but the app is not closed. 当用户触摸“最新记录”按钮但未关闭应用程序时,将调用onPause方法。 onStop method same of onPause . onStop方法与onPause相同。

So... How to know when the user close the app? 所以...如何知道用户何时关闭应用程序?

Thank you 谢谢

You can write a method to check if the app is running in background or not. 您可以编写一种方法来检查应用程序是否在后台运行。

private boolean isAppIsInBackground(Context context) {
        isInBackground = true;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : processInfo.pkgList) {
                        if (activeProcess.equals(context.getPackageName())) {

                            isInBackground = false;
                        }
                    }
                }
            }
        } else {
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            if (componentInfo.getPackageName().equals(context.getPackageName())) {
                isInBackground = false;

            }
        }

        return isInBackground;
    }

This method returns true if the app is running in background.Then you can enable or disable bluetooth accordingly. 如果应用程序在后台运行,则此方法返回true。然后您可以相应地启用或禁用蓝牙。 Also you can set a flag in your onResume and onPause methods of the BaseActivity which is extended by all other activitues so that you can check the flags to know whether app is running or not. 您还可以在BaseActivity的onResume和onPause方法中设置一个标志,该标志由所有其他活动扩展,以便您可以检查标志以了解应用程序是否正在运行。

Make a BaseActivity and override its OnPause() method and extend every Activity from BaseActivity . 创建一个BaseActivity并重写其OnPause()方法,并扩展BaseActivity中的每个Activity。

I would also like to mention that onPause() is fired even when switching between multiple activities, so you'd need to track when it's pausing to go to another screen. 我还要提及的是,即使在多个活动之间切换时也会触发onPause(),因此您需要跟踪何时暂停进入另一个屏幕。

Reference : http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle 参考: http : //developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

如果要在用户退出活动时禁用蓝牙,请在onBackPressed()其禁用。

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

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