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

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

Related

How can i verify if certain app is running on Android?

i trying to make an android app, that verify if certain app is running, if the certain app is not running my app have to open it, but i donĀ“t know how to do for to verify if the certain app is running? can somebody help me please...
you can use TaskManager see this Sample Code
http://forum.xda-developers.com/attachment.php?attachmentid=2955965&stc=1&d=1412353638
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
/**recentTasks.get(0) Activity that is on top >> your app and number 1 will next **/
String pack = recentTasks.get(0).baseActivity.toShortString();
also you can get info of running app with UsageStats like this
try{
AppOpsManager appOps = (AppOpsManager) getSystemService(APP_OPS_SERVICE);
int mode = appOps.checkOp("android:get_usage_stats", android.os.Process.myUid(), getPackageName());
if(mode == AppOpsManager.MODE_ALLOWED) {//should turn on UsageStats
UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(USAGE_STATS_SERVICE);
List<UsageStats> queryUsageStats = mUsageStatsManager
.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
System.currentTimeMillis() - 1000*60, //begin
System.currentTimeMillis()); //end
Collections.sort(queryUsageStats, new LastTimeLaunchedComparator());
try{
CustomUsageStats customUsageStats = new CustomUsageStats();
customUsageStats.usageStats = queryUsageStats.get(1);
String previousAppPack = customUsageStats.usageStats.getPackageName();
//the package of app before your app
}
catch (Exception e){}
}

I want to create an application that shows all the applications that were launched

Time and Name of application opened
final PackageManager pm = getPackageManager();
packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo packageInfo : packages )
{
String name=packageInfo.getClass().getName();
Toast.makeText(ListActivity.this, ""+ name, 0).show();
}
Try this to get the list of running apps.
ActivityManager activity_manager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
Use getRunningTasks() method from ActivityManager.
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+"");
}
Add this permission in the manifest file.
<uses-permission android:name="android.permission.GET_TASKS" ></uses-permission>

How to get PID from package name?

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

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!

How to get App info from RecentTaskInfo?

Currently, I can only get the IDs of recent tasks, as below:
final List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(Integer.MAX_VALUE, 1);
for (int i = 0; i < recentTasks.size(); i++)
{
System.out.println(""+recentTasks.get(i).id);
}
But how to get other info? like package_name, main_activity and so on?
Also I found some IDs are -1. What does it mean? Can we get info of these tasks?
-1 means the task is NOT currently running.
You can use recentTaskInfo.baseIntent to get the Intent that was used to launch the task, then e.g. get the package name using intent. getComponent().getPackageName().
here, I used this code for bring running app to foreground from background.
I hope this help you...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(Integer.MAX_VALUE, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
RecentTaskInfo recentTaskInfo = null;
for (int i = 0; i < recentTasks.size(); i++)
{
if (recentTasks.get(i).baseIntent.getComponent().getPackageName().equals(packageName)) {
recentTaskInfo = recentTasks.get(i);
break;
}
}
if(recentTaskInfo != null && recentTaskInfo.id > -1) {
activityManager.moveTaskToFront(recentTaskInfo.persistentId, ActivityManager.MOVE_TASK_WITH_HOME);
return;
}
}

Categories

Resources