Two instances of my android application are running…How to avoid this?

前端 未结 6 1850
一生所求
一生所求 2020-12-05 12:13

Here is my problem -

  1. I copied my .apk file onto phone memory card and launch my application clicking on it and it allows me to install my application.I ins

6条回答
  •  余生分开走
    2020-12-05 12:56

    // put below code in your launcher activity before call super and setcontentview()

    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);

        // get the info from the currently running task
        List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(10);
        boolean alreadyTask=false;
        for(ActivityManager.RunningTaskInfo info : taskInfo){
            ComponentName componentInfo = info.topActivity;
            String value= componentInfo.getPackageName();
            if(value.contains(getPackageName()) && !info.topActivity.getClassName().contains(getPackageName()+".LauncherActivity")){
                alreadyTask=true;
                Log.i(TAG, "second instance found!!!");
                break;
            }
        }
    
        if(alreadyTask){
            finish();
        }
    

提交回复
热议问题