How to get PID from package name?

后端 未结 1 939
悲&欢浪女
悲&欢浪女 2021-01-06 09:38

I need create a function but I don\'t know how to get the PID of the app.

// here return -1 but I need PID to kill process
Integer PID1= android.os.Process.g         


        
相关标签:
1条回答
  • 2021-01-06 10:05

    try

    restartPackage code

    ActivityManager aM = (ActivityManager);
    getApplicationContext().getSystemService(getApplicationContext().ACTIVITY_SERVICE);
    aM.restartPackage("com.android.email");
    

    Kill BackGround Process Code

    ActivityManager aM = (ActivityManager)
    getApplicationContext().getSystemService(Catalogue.content_holder.getApplicationContext().ACTIVITY_SERVICE);
    aM.killBackgroundProcesses("com.android.email");
    

    Here is code which fetches all running Application and check wether email app is already running or not , if it is running then kill that process

    ActivityManager manager =  (ActivityManager) getApplicationContext.getSystemService(getApplicationContext.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> activityes = ((ActivityManager)manager).getRunningAppProcesses();
    
    for (int iCnt = 0; iCnt < activityes.size(); iCnt++){
    
        System.out.println("APP: "+iCnt +" "+ activityes.get(iCnt).processName);
    
        if (activityes.get(iCnt).processName.contains("com.android.email")){
            android.os.Process.sendSignal(activityes.get(iCnt).pid, android.os.Process.SIGNAL_KILL);
            android.os.Process.killProcess(activityes.get(i).pid);
            //manager.killBackgroundProcesses("com.android.email");
    
            //manager.restartPackage("com.android.email");
    
            System.out.println("Inside if");
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题