Get number of installed apps on an Android device? - java

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

Related

No IMEI for Android Developers in Android 10

As Android is serious about security and trying to make new android versions more secure, its becoming tough for developers to keep up-to date with new security features and find old methods alternatives to make their app compatible with old features.
This question is about IMEI in New Android 10!
The old method was super easy to get IMEI number by using below code
String deviceId = "";
if (Build.VERSION.SDK_INT >= 26) {
if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
deviceId = telMgr.getMeid();
} else if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
deviceId = telMgr.getImei();
} else {
deviceId = ""; // default!!!
}
} else {
deviceId = telMgr.getDeviceId();
}
In New Android 10 it is now restricted to get IMEI number. According to android documentation
apps must have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to access the device's non-resettable identifiers, which include both IMEI and serial number.
The problem is that when we try to ask run time permissions with
android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE
My compiler does not recognize this permissions and i got error on this line but when i ask this permission in manifest file it does recognize this line but through warning that this permission is only for system apps.
I want to make my app compatible with Android 10 and want to get IMEI. How can i get IMEI number in Android 10 without becoming device owner or profile owner ?
Only device owner apps can read unique identifiers or your app must be a system app with READ_PRIVILEGED_PHONE_STATE. You can't ask this permission for a normal app.
Android 10 Restricted developer to Access IMEI number.
You can have a alternate solution by get Software ID. You can use software id as a unique id. Please find below code as i use in Application.
public static String getDeviceId(Context context) {
String deviceId;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
deviceId = Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID);
}else {
final TelephonyManager mTelephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null) {
deviceId = mTelephony.getDeviceId();
} else {
deviceId = Settings.Secure.getString(
context.getContentResolver(),
Settings.Secure.ANDROID_ID);
}
}
return deviceId;
}
To get a unique identifier you can use FirebaseInstanceId.getInstance().getId(); or String uniqueID = UUID.randomUUID().toString();. Have a look here for best practices https://developer.android.com/training/articles/user-data-ids#java

I've got this method to get the data mobile used for every installed app

Hello this is my method to get the used mobile data for every installed application in an android phone but it only get me the data used by my app not all the apps
I need it to get the data mobile used by every app installed in the phone
void OthernetworkUsage() {
List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_PERMISSIONS);
for(PackageInfo p : packs){
// if(!isSystemPackage(p)){
if(usesInternet(p)) {
long received = TrafficStats.getUidRxBytes(p.applicationInfo.uid);
long sent = TrafficStats.getUidTxBytes(p.applicationInfo.uid);
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
int uid = p.applicationInfo.uid;
Toast.makeText(this, "uid: "+uid+"/Kb - name: "+appName+": Sent = "+sent/1024+"Kb, Rcvd = "+received/1024+"Kb", Toast.LENGTH_SHORT).show();
}
//}
}
}
I really need help in that case.

How to count number of times any application opened in my android mobile?

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

How to get running package names for Android L

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

Execution count of installed applications

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

Categories