android退出APP问题?
高洛峰
高洛峰 2017-04-18 09:06:09
0
4
494
我在一个Android APP里面给notification设置了个按钮,希望点击之后能够退出APP,停止一切用到的服务(例如:科大讯飞的语音识别服务,自己启动的一些service,打开过的activity)。
我用了以下两种方法
System.exit(0)
Process.killProcess(Process.myPid()) 

但是都不能完全退出APP,可以杀死service,也可以停止语音识别等服务,但是没有退出activity,按下home键把应用后台了之后点击那个停止按钮之后打开APP没有从头开始启动第一次打开的launch页面,而是直接打开了刚刚后台了的时候开着的activity。

我该怎么准确退出这个APP回到那种什么都没开过的那种初始状态。

谢谢大神们 !

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(4)
洪涛

You can control all activities in your own queue. The code is as follows:

public class ActivityUtils {

    private static List<Activity> activities = new ArrayList<>();
    
    public static void addActivity(Activity activity) {
        activities.add(activity);
    }

    /**
     * 关闭所有Activity
     */
    public static void finishAllActivity() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }
}

Add in the onCreate method in your BaseActivity:

ActivityUtils.addActivity(this);

Then call where you need

ActivityUtils.finishAllActivity();

Hope it helps you

左手右手慢动作

Listen to the open activities in the application and kill all activities after clicking the button

application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {}
左手右手慢动作

It seems that no one has answered, so I will post my own solution.
Still don’t know how to completely exit the APP, so I first clear the activity stack and then kill the process.

ActivityManager activityMgr= (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
 activityMgr.killBackgroundProcesses(getPackageName());
 android.os.Process.killProcess(android.os.Process.myPid()); 
System.exit(0);
左手右手慢动作

Define an Activity queue, add each activity when it is created, and provide a static method to finish all activities in the queue

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template