简体   繁体   English

Android:杀死应用程序中的所有进程

[英]Android: Kill all processes in an application

In my application I have an "Application" and several Activities. 在我的应用程序中,我有一个“应用程序”和几个活动。 I would like to kill my application after a specific time (ex. 3 minutes) after user pressed the Home button. 我想在用户按下主页按钮后的特定时间(例如3分钟)后终止我的应用程序。 I'm able to finish the "Application"s process but as long as my activities are alive, they tries to start from their last state. 我能够完成“应用程序”的过程,但只要我的活动还活着,他们就会尝试从最后一个状态开始。

Thread isOnDisplayThread = new Thread(new Runnable() {



            @Override
            public void run() {
                Timer mTimer = new Timer();

                mTimer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        Log.d(TAG, (isApplicationOnDisplay) ? "Application on display" : "APPLICATION ON BACKGROUND");

                        if (!isApplicationOnDisplay) {
                            notOnDisplayTime++;
                            if (notOnDisplayTime >= 10) {

                                Process.killProcess(Process.myPid());
                            }
                        } else {
                            notOnDisplayTime = 0;
                        }

                    }
                }, 0, 1000);

            }
        });

        isOnDisplayThread.run();

PS isApplicationOnDisplay is a static boolean controlled by every Activity's onPause() and onResume() methods. PS isApplicationOnDisplay是一个静态布尔值,由每个Activity的onPause()onResume()方法控制。

Is there any way to kill all related activities of an application? 有没有办法杀死应用程序的所有相关活动?

(I can't understand why everybody tries to kill his own application...) (我无法理解为什么每个人都试图杀死自己的应用......)

Killing an application should never be done by an application (though sometimes an task manager has his advantages). 应用程序永远不应该杀死应用程序(尽管有时任务管理器有其优势)。

I'm able to finish the "Application"s process but as long as my activities are alive, they tries to start from their last state. 我能够完成“应用程序”的过程,但只要我的活动还活着,他们就会尝试从最后一个状态开始。

You have full control about the state handling, you decide if an activity continue with the latest state or not. 您可以完全控制状态处理,您可以决定某个活动是否继续使用最新状态。 If you don't want the current behavior, change it , but don't try to kill your application. 如果您不想要当前行为,请更改它 ,但不要尝试终止您的应用程序。

To make it short: The Android OS decides if an application should be killed/stopped or not 简而言之: Android操作系统决定是否应该杀死/停止应用程序

Thats how android is designed and how it should be used... 多数民众赞成如何设计Android以及如何使用它......

我建议你,完成所有活动,关闭所有线程,不要干扰背景...... :))

All the activities at once kill is not possible. 所有的活动都不可能立刻被杀死。

you should do two things to kill the activities and services 你应该做两件事来杀死活动和服务

1) @Override public void onBackPressed() { 1)@Override public void onBackPressed(){
onDestroy(); 的onDestroy();
super.onBackPressed(); super.onBackPressed(); } }

2) you should apply at change Intent onDestroy(); 2)你应该在改变Intent onDestroy()时申请;

3)and if neccessary you should apply @Override public void onConfigurationChanged(Configuration newConfig) 3)如果需要,你应该应用@Override public void onConfigurationChanged(Configuration newConfig)

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

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