Get run duration of running task - android

After I get a list of running tasks, I can get almost any information I would need. However, I couldn't find how to get how long the task has been running. I even tried the metaData Bundle that gets returned with the ActivityInfo, but that is null even though I explicitly request it. Is this possible at all?
I get the list of tasks as follows:
ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
PackageManager pm = getPackageManager();
for (RunningTaskInfo thisInfo : list) {
try {
ComponentName componentName = thisInfo.baseActivity;
ActivityInfo activityInfo = pm.getActivityInfo(componentName, PackageManager.GET_META_DATA);
// this is null for some reason
Bundle metaData = activityInfo.applicationInfo.metaData;
} catch (NameNotFoundException e) {
Log.e("AppChecker", "Oops");
e.printStackTrace();
}
}

Related

Receive other applications' usage time data

I'm working on an android project. I need to receive other applications' usage time. I simply just need how many application is running and what are they. How can I access these kinds of data?
I found some articles about receiving another's SQLite DB but I really don't need this.
You may follow the steps:
Retrieve the list of the tasks that are currently running by calling
getRunningTasks()
Go through each of the task and retrieve the PackageName
Get AppName from the PackagName
Add the AppName in a list
Here is the implementation:
private List<String> getRunningAppList()
{
Set<String> appNameList = new HashSet<>();
PackageManager pm = getPackageManager();
ActivityManager aManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// Get the list of the tasks that are currently running
List<ActivityManager.RunningTaskInfo> infoList = aManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo taskInfo : infoList)
{
// Get the packageNAme
String pkgName = taskInfo.topActivity.getPackageName();
String appName = null;
// Get the appName
try
{
appName = pm.getApplicationLabel(pm.getApplicationInfo(pkgName,PackageManager.GET_META_DATA)).toString();
} catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
// Adding the appName
appNameList.add(appName);
}
return new ArrayList<>(appNameList);
}
Using UsageStats, UsageStatsManager is simply the best approach for this topic.
https://developer.android.com/reference/android/app/usage/UsageStats
UsageStats.getLastTimeUsed() informs you about a specific application's last usage time by date format.

Detect which app has been launched in android

How to detect which app has been launched by user in my app i.e my application should get notified when Whatsapp is launched by user even if my app is not running in foreground or background.
hike messenger has achieved same functionality with accessibility service.
How can I solve this problem ?
Thanks in advance!!
Depending on the Android version running your application, you will have to use different methods.
On Pre-Lollipop devices, it is pretty straight-forward:
String[] result = new String[2];
List<ActivityManager.RunningTaskInfo> runningTasks;
ComponentName componentInfo;
runningTasks = activityManager.getRunningTasks(1);
componentInfo = runningTasks.get(0).topActivity;
result[0] = componentInfo.getPackageName();
result[1] = componentInfo.getClassName();
If you are on a Lollipop or newer device, you have to use UsageStatsManager class, which requires your application to be granted specific permissions
//no inspection ResourceType
UsageStatsManager mUsageStatsManager = (UsageStatsManager)context.getSystemService("usagestats");
long time = System.currentTimeMillis();
// We get usage stats for the last 10 seconds
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*10, time);
// Sort the stats by the last time used
if(stats != null) {
SortedMap<Long,UsageStats> mySortedMap = new TreeMap<>();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
}
if(mySortedMap != null && !mySortedMap.isEmpty()) {
return mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
return null;
This will tell you if your apps has been granted permissions:
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
return (mode != AppOpsManager.MODE_ALLOWED);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
And finally this will launch the Android permission granting activity for the user:
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
activity.startActivity(intent);
Hope that helps
Try this code:
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++)
{
if(procInfos.get(i).processName.equals("put the package name here"))
{
Toast.makeText(getApplicationContext(), "Notify Message", Toast.LENGTH_LONG).show();
}
}
No, this is not really possible using the public SDK.
You can get the current running process by ActivityManager#getRunningAppProcesses But it is definitely impossible to get notified .However, it isn't the most accurate, or efficient method

How to get names of background running apps in android?

For example, I have "music" and " speech recorder" run in my AVD background. I want to make a AlertDialog that can show the names " music, speech recorder". Who can give me some codes? Thank you.
this may help you...
public static HashSet<String> getRunningApps(Context context) {
final HashSet<String> hashSet = new HashSet<String>();
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final PackageManager packageManager = context.getPackageManager();
List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo runningTaskInfo : runningTasks) {
String packageName = runningTaskInfo.baseActivity.getPackageName();
try {
String appName = packageManager.getApplicationInfo(packageName, 0).loadLabel(packageManager).toString();
hashSet.add(appName);
} catch (NameNotFoundException exception) {
// handle Exception
}
}
return hashSet;
}
You can list all processes with the followings lines:
ActivityManager localActivityManager = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
List RunningServiceInfo services = localActivityManager .getRunningServices(100);
And from RunningServiceInfo you can get details for the process.
Another way is using getRunningAppProcesses.

How can I determine permission of Running apps programmatically in android?

I want to see the permission of running apps of android in my software.
For this reason ,I have the following code :
List<App> apps = new ArrayList<App>();
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
PackageManager packageManager = getPackageManager();
List<RunningAppProcessInfo> l = am.getRunningAppProcesses();
Iterator<RunningAppProcessInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
int row_count = 0 ;
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
App app = new App();
app.setTitle(c.toString());
app.setPackageName(l.get(row_count).processName);
PackageInfo packageInfo = packageManager.getPackageInfo(l.get(row_count).processName, PackageManager.GET_PERMISSIONS);
String[] reqPermission= packageInfo.requestedPermissions;
app.set_Permission_Info(reqPermission);
// app.setVersionName(p.versionName);
// app.setVersionCode(p.versionCode);
// CharSequence description = p.applicationInfo.loadDescription(packageManager);
// app.setDescription(description != null ? description.toString() : "");
row_count++;
// app.setSize(p.s)
apps.add(app);
}
catch(Exception e){}
But there is a problem.
When I run my apps ,I find that the app name and app's package name are not consistent . Why has this problem introduced?
The main problem is described follow:
Let us suppose an apps named "EBOOK_Reader" and "Camera" is running in my device . The package name is "com.a.b" and "com.c.d" respectively. The problem of this code is the appropriate package name is not with appropriate apps name .
It shows the package name Of "com.a.b" to "Camera " and "com.c.d" to "EBOOK_Reader" which is not desired .
Any idea of how can the problem be solved?
ThankYou
This is correct and Running:
PackageManager mPm = getPackageManager();
List <PackageInfo> appList=mPm.getInstalledPackages(PackageManager.GET_PERMISSIONS|PackageManager.GET_RECEIVERS|
PackageManager.GET_SERVICES|PackageManager.GET_PROVIDERS);
for (PackageInfo pi : appList) {
System.out.println("Process Name: "+pi);
// Do not add System Packages
if ((pi.requestedPermissions == null || pi.packageName.equals("android")) ||
(pi.applicationInfo != null && (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0))
continue;
for (String permission : pi.requestedPermissions) {
//Map<String, String> curChildMap = new HashMap<String, String>();
//System.out.println("############ "+permission);
try {
PermissionInfo pinfo = mPm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
CharSequence label = pinfo.loadLabel(mPm);
CharSequence desc = pinfo.loadDescription(mPm);
System.out.println("$$$$$ "+label+"!!!!!! "+desc);
} catch (NameNotFoundException e) {
Log.i(TAG, "Ignoring unknown permission " + permission);
continue;
}
}
}
The app name and app's package name are normally different. You better use the package name as this is unique throughout the device.
Update:
Now I understand your problem. Thanks for clarifying. It is because of the variable row_count. Basically you're are using two different iterator variables. That's why your getting 2 different results. You don't need row_count because you already have interator for i.
Try the updated code below:
Basically l.get(row_count).processName was replaced by info.processName.
List<App> apps = new ArrayList<App>();
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
PackageManager packageManager = getPackageManager();
List<RunningAppProcessInfo> l = am.getRunningAppProcesses();
Iterator<RunningAppProcessInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
// int row_count = 0 ; // no need for this. feel free to delete
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
App app = new App();
app.setTitle(c.toString());
app.setPackageName(info.processName);
PackageInfo packageInfo = packageManager.getPackageInfo(info.processName, PackageManager.GET_PERMISSIONS);
String[] reqPermission= packageInfo.requestedPermissions;
app.set_Permission_Info(reqPermission);
// app.setVersionName(p.versionName);
// app.setVersionCode(p.versionCode);
// CharSequence description = p.applicationInfo.loadDescription(packageManager);
// app.setDescription(description != null ? description.toString() : "");
//row_count++; // no need for this. feel free to delete
// app.setSize(p.s)
apps.add(app);
}
catch(Exception e){}
Process names are not bound to the application package name. They happen to be the same by default, as a convenience. However, each app is free to change its process name in its manifest using the android:process attribute, or to spawn more processes with different names for various components.
And in even more advanced scenarios, multiple applications can share the same process.
In particular, what this means is you can't use the process name to get the application(s) that are running currently. You should instead iterate over the list of packages that are loaded in that process using the RunningAppProcessInfo.pkgList field instead. Keep in mind that it is an array, and can contain more than one application package name. (See the note about the advanced scenarios above)
On a separate note, as the documentation for the getRunningAppProcesses() states:
Note: this method is only intended for debugging or building a user-facing process management UI.

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