How can i check if my app is running

感情迁移 提交于 2019-12-02 17:23:27

问题


  1. How can i check if my android app is already running to prevent double launch?

  2. How can i make "hard exit" to prevent my app running on background?


回答1:


  1. It is not possible to have "double launch". If you application is running already, if you try to launch another instance, than you'll resume the first launch instance.

  2. You can finish activity by adding .finish() in all scenarios when application can be in onPaused() sequence of the lifecycle or add finish() in onPaused()




回答2:


This may help you

ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);

if((serviceList.size() > 0)) {
    boolean found = false;

    for(int i = 0; i < serviceList.size(); i++) {
        RunningServiceInfo serviceInfo = serviceList.get(i);
        ComponentName serviceName = serviceInfo.service;

        if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
            //Your service or activity is running
            found = true;
            break;
        }
    }
    if(found) {
        //Close your app or service
    }
}


来源:https://stackoverflow.com/questions/6858414/how-can-i-check-if-my-app-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!