Monitoring app launches on android - 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

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){}
}

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.

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!

Get Current Running Process in Android

It's about Currently Running Process Info.
I would like to capture the vivid name or the same name which appeared under icon of the current running process.
While using RunningTaskInfo or RunningProcessInfo , I can only get the complex information like com.android.browser.
Actually, I want to get the names like Brower, Clock, Contact etc.
How can I achieve this? Please direct to the full tutorial link or with fully explanation codes.
PS: I am just a beginner and I am sure that I've already explored the Android Developer site.
Get the current RunningAppProcessInfo from pid
public static String getCurrentProcessName(Context context) {
// Log.d(TAG, "getCurrentProcessName");
int pid = android.os.Process.myPid();
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses())
{
// Log.d(TAG, processInfo.processName);
if (processInfo.pid == pid)
return processInfo.processName;
}
return "";
}
Once you get the package name (such as com.android.browser), simply use that name with PackageManager:
PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(name, 0);
CharSequence name = pm.getApplicationLabel(ai);
You'll need to handle some exceptions, but you will get the real app names from this.

Categories

Resources