How to get PID from package name? - android

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.getUidForName("com.android.email");
android.os.Process.killProcess(PID1);

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

Related

Detect if another app is running

Is it possible on Android to detect if another app is running? I need to trigger some action in my app when a Map application (like Waze or Google Maps) is running.
You can use ActivityManager class:
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < recentTasks.size(); i++)
{
Log.d("Executed app", "Application executed : " +recentTasks.get(i).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(i).id+"");
}
Please be aware that this capability was deprecated in API level-21.
An alternative could be getRunningAppProcesses() method of ActivityManager.

Detect whether process is system process or not

I am using this method to get running processes
private void getRunningProcess() {
{PackageManager pm=getActivity().getPackageManager();
final ActivityManager manager = (ActivityManager)getActivity().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();
if (runningProcesses != null && runningProcesses.size() > 0) {
setListAdapter(new ListAdapter(getActivity(), runningProcesses));
}
}
I am getting all processes but I want to show only non-system processes.
Any help will be appreciated.
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> run = am.getRunningAppProcesses();
try it!

Android: Killing third a app

I've a problem with killing third party apps from my application. Here's the code:
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
if (appProcess.pkgList[0].equalsIgnoreCase("com.adobe.air")) {
Log.v("ACTIVITY FOUND", "" + appProcess.pkgList[0]
+ " - " + appProcess.pid);
activityManager.killBackgroundProcesses("com.adobe.air");
activityManager.restartPackage("com.adobe.air");
android.os.Process.killProcess(appProcess.pid);
}
}
}
Log.v("RUN", "----------------------------------");
And in AndroidManifest I added the permissions android.permission.KILL_BACKGROUND_PROCESSES and android.permission.RESTART_PACKAGES.
In Log I can correctly read the message when the package com.adobe.air is running, but killBackgroundProcesses, restartPackage and KillProcess have no success force closing the app itself. What's wrong?
You can use Process.killProcess(int pid) to kill processes that have
the same UID with your App.
You can use
ActivityManager.killBackgroundProcesses(String packageName),with
KILL_BACKGROUND_PROCESSES permission in your manifest(for API >= 8)
or ActivityManager.restartPackage (String packageName)(for API < 8)
to kill specified process,except of forground process.

Monitoring app launches on android

I need to create an app that is able to check when other apps are opened or closed ("on resume" or "on pause") on the device. I need to check the time of use of each app.
Can I do it without root access in the device?
try this code
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++){
if(procInfos.get(i).processName.equals("com.android.browser")) {
Toast.makeText(getApplicationContext(), "Browser is running", Toast.LENGTH_LONG).show();
}
}
find the package name using download package name viewer
https://play.google.com/store/apps/details?id=com.gijoon.pkgnameviewer&hl=en

How to get pid of android application without using adb shell?

How can I get an android application pid without using adb shell? Is there any API to get pid.
any help will be appreciated
As every application has its own process id, one can get it by
int pid = android.os.Process.myPid();
This also works:
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
int processid = 0;
for (int i = 0; i < pids.size(); i++) {
ActivityManager.RunningAppProcessInfo info = pids.get(i);
if (info.processName.equalsIgnoreCase("here your package name")) {
processid = info.pid;
}
}

Categories

Resources