I am interested in getting values of already installed packages in android. I am trying to find the value how many times were the already installed packages executed (closed and open). I am aware i can do that for my application from sharepreferences but how to do for packages that are already there? I already have the list of the packages installed using PackageManager.
Thanks in advance
The PackageInfo class, which can be retrieved for Packages using the PackageManager can you give you information about first install time and last update time. But there doesn't seem to be any way to find out how many times it was launched, etc. I'm not even sure the system keeps track of that information. Check out http://developer.android.com/reference/android/content/pm/PackageInfo.html
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager packageManager = mContext.getPackageManager();
List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
try this.
I dont think this is possible from regular applications. Though if you have root access you can execute
dumpsys usagestats
and parse the output.
Or you could use the Usagestats Service that already does the tracking. But again you need root access for this.
PackageManager manager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));
if (apps != null) {
final int count = apps.size();
if (mApplications == null) {
mApplications = new ArrayList<ApplicationInfo>(count);
}
}
for (int i = 0; i < count; i++) {
ApplicationInfo application = new ApplicationInfo();
ResolveInfo info = apps.get(DEFAULT_KEYS_SEARCH_LOCAL);
application.title = info.loadLabel(manager);
application.setActivity(new ComponentName(
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name),
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
application.icon = info.activityInfo.loadIcon(manager);
// mApplications.add(application);
}
this will give u the count of the all applications installed
Related
In MainActivity I want to know the current Top Activity opened inside App.
For pre-lollipop I used following
ActivityManager am = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
On other posts I found, I should use:
getAppTasks() and gettaskinfo
But I am not able to find any complete example demonstrating how to use the above. Any help is thankful. Regards.
Edit:
Current code:
if (Build.VERSION.SDK_INT >= 23) {
cn = am.getAppTasks().get(0).getTaskInfo().topActivity;
}else{
cn = am.getRunningTasks(1).get(0).topActivity;
}
am.getRunningTasks is deprecated in API 21. And am.getAppTasks#topActivity is available in API 23. So updated question is how to handle it for API 21 and 22.
ActivityManager mgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (mgr != null) {
List<ActivityManager.AppTask> tasks = mgr.getAppTasks();
String className;
if (tasks != null && !tasks.isEmpty()) {
className = tasks.get(0).getTaskInfo().topActivity.getClassName();
}
}
Credits go to https://www.intertech.com/Blog/android-5-api-changes-getapptasks/
I want to develop an android application where it counts the number of times I have opened another application. Say it should count a number of times I have opened Whatsapp or may be Facebook. How can I achieve this through another android application? How to observe the activity, behavior of other applications in android.
first
1. create a db ( local android SQLite)
then
2. get a list of all the installed applications on the device.Use Android Package Manager with getInstalledApplications()
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d("Installed Apps", "Installed package :" + packageInfo.packageName + " Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}
save package names and launcher activities in your db
get list of recently launched apps and increment the counter of your db or what you like to do
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RecentTaskInfo> recentTasks = activityManager.getRecentTasks(Integer.MAX_VALUE,ActivityManager.RECENT_WITH_EXCLUDED);
for (int i = 0; i < recentTasks.size(); i++)
{
String LocalApp = recentTasks.get(i).baseIntent.toString();
int indexPackageNameBegin = LocalApp.indexOf("cmp=")+4;
int indexPackageNameEnd = LocalApp.indexOf("/", indexPackageNameBegin);
String pckge = LocalApp.substring(indexPackageNameBegin, indexPackageNameEnd);
Log.d("Executed app", "Application executed : " +pckge);
}
I'm making an Android launcher as an introduction to making Android apps for myself, and part of my design requires me to know how many apps are installed on a user's device, and preferably count only the ones which are normal apps that can be launched. I wish to store this number of apps in a global, integer variable. With only this goal in mind, what is the simplest way of just retrieving this number as that variable?
You could use the getInstalledApplications() method of PackageManager.
From the documentation:
Return a List of all application packages that are installed on the
device. If flag GET_UNINSTALLED_PACKAGES has been set, a list of all
applications including those deleted with DONT_DELETE_DATA (partially
installed apps with data directory) will be returned.
Something like this should work:
int numberOfInstalledApps = getPackageManager(0).getInstalledApplications().size();
To filter out the system apps you could do something like this:
int numberOfNonSystemApps = 0;
List<ApplicationInfo> appList = getPackageManager().getInstalledApplications(0);
for(ApplicationInfo info : appList) {
if((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
numberOfNonSystemApps++;
}
}
The all non-system apps have a launch Intent so you just need to fetch the list of all apps and check, how many of them has a launch intent or if not then that app will be a system app.The list of all apps can easily be retrieved by package manager and then we go through the information of all apps while looking for the available launch intent.
As suggested by Darshan Patel : #Brad Larson♦
PackageManager pm = getPackageManager();
int nonSysAppsCount=0;
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo packageInfo:packages){
if( pm.getLaunchIntentForPackage(packageInfo.packageName) != null ){
String currAppName = pm.getApplicationLabel(packageInfo).toString();
nonSysAppsCount++;
//This app is a non-system app
}
else{
//System App
}
}
If you are looking for the non-system applications installed on a given device, you can do the following :
public static ArrayList<String> getInstalledApps() {
ArrayList<String> appList = new ArrayList<>();
List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);
for (int i=0; i < packList.size(); i++) {
PackageInfo packInfo = packList.get(i);
if ( (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
appList.add(appName);
Log.e("App >" + Integer.toString(i), appName);
}
}
return appList;
}
So, you can the list by doing :
int number = getInstalledApps().size();
Additionally, you can start any of the applications by calling :
Intent myIntent = new Intent(Intent.ACTION_MAIN, null);
myIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List appsList = context.getPackageManager().queryIntentActivities(myIntent, 0);
So we all know that the getRecentTasks() and getRunningTasks() on ActivityManager are now deprecated and will return a reduced result set on Android L and higher devices.
Alternative to getRunningTasks in Android L
https://code.google.com/p/android-developer-preview/issues/detail?id=29
However, I am trying to find a solution to keep my App Locker app alive on Android L. I need the package name of the top Activity in order to show the lock screen when the users opens/launches the locked app.
It is very similar to this app: https://play.google.com/store/apps/details?id=com.domobile.applock&hl=en
Currently I am using this code:
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager
.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
String activityOnTop = ar.topActivity.getPackageName();
But it won't work in Android L, so I am not sure what exactly to do...
How can I implement something like this in Android L?
Unfortunately, there is no equivalent in Android 5.0: it is not possible to get the top most activity, nor is it possible get any callback when a new application/activity is launched in realtime.
please refer this link.
i hope it will helpful to you, in my project it work. thanks.
Try this line of code for Android L
ActivityManager activityManager = (ActivityManager) getSystemService (Context.ACTIVITY_SERVICE);
String packageName = activityManager.getRunningAppProcesses().get(0).processName;
and please note that this works only for Lollipop devices .. If you want any platform support then add the following code.
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP)
{
String packageName = activityManager.getRunningAppProcesses().get(0).processName;
}
else if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)
{
String packageName = ProcessManager.getRunningForegroundApps(getApplicationContext()).get(0).getPackageName();
}
else
{
String packageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
}
Using AlarmManager I can set alarm for any time from the android App. But is there any way to list all the alarms set by me. What should be my approach towards that, as AlarmManager do not provide with such methods. Should I go for saving the alarm as a file in the memory.?
Please Help me with this.
Visiting all the possible links I came to a solution that creating an app that would retrieve the alarms set on the OS level is not possible. Ya.. to some extent it would be possible but in many cases it would be machine dependent.
So better option is to save your Alarms in your database.
A. There is a good explanation on this post regarding this. There are no guarantees that the AlarmClock app will be on every device your app is installed on. For example, many of the HTC phones replace it with HTC's own "World Clock" app.
However, assuming the stock AlarmClock app is present, you should be able to get a cursor from its content provider. See this project as an example.
B. You have to create a layout for items of the ListView.
You can find tutorials about this on Internet : http://www.vogella.com/articles/AndroidListView/article.html http://codehenge.net/blog/2011/05/customizing-android-listview-item-layout/
c.
final String tag_alarm = "tag_alarm";
Uri uri = Uri.parse("content://com.android.alarmclock/alarm")
Cursor c = getContentResolver().query(uri, null, null, null, null);
Log.i(tag_alarm, "no of records are" + c.getCount());
Log.i(tag_alarm, "no of columns are" + c.getColumnCount());
if (c != null) {
String names[] = c.getColumnNames();
for (String temp : names) {
System.out.println(temp);
}
if (c.moveToFirst()) {
do {
for (int j = 0; j < c.getColumnCount(); j++) {
Log.i(tag_alarm, c.getColumnName(j) + " which has value " + c.getString(j));
}
} while (c.moveToNext());
}
}