search

Home  >  Q&A  >  body text

android退出APP问题?

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

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

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

谢谢大神们 !

高洛峰高洛峰2772 days ago470

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 09:08:09

    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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:08:09

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

    application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {}

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:08:09

    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);

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:08:09

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

    reply
    0
  • Cancelreply