When I open the Android main menu on my Android smartphone, I get a set of apps like Youtube, Calculator, Email clients etc. No system stuff or any libraries are visible there.
To retrieve these apps programamtically, I do:
PackageManager.getInstalledApplications(flag: Int)
where I get a list of ApplicationInfo, which also contains alot more than mentioned installed standard apps. What flag do I have to set to get only the same apps, which I see when I swipe up on my Smartphone?
val mainIntent = Intent(Intent.ACTION_MAIN, null)
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER)
val appList = context.getPackageManager().queryIntentActivities( mainIntent, 0)
You can try this to get all user apps (and the ones your launcher shows)
You might need to add additional permissions in your manifest above Android 10 though
Edit: If you want ApplicationInfo instead of ResolveInfo in your list you can retrieve it like this:
appList[your_index].activityInfo.applicationInfo
Related
I want to implement a listview showing android applications with their internet usage. Fir this, first i have to list all the apps, i have done this using PackageManager, like this:
packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_META_DATA);
apkList = (ListView) findViewById(R.id.applist);
apkList.setAdapter(new ApkAdapter(this, packageList, packageManager));
But this code lists all system apps as well like : Android Sytem, Calculator,Calender, Status Bar, Live Wallpapers etc. which doesnt look appropriate. I tried to filter system apps using:
/*To filter out System apps*/
for(PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
if(!b) {
packageList1.add(pi);
}
}
But then the code displays only installed apps, like whatsapp, tango, foursquare etc. It does not show apps like gmail, facebook, browser,maps.
Can anybody suggest how should i write the code that only displays list of application that actually use the internet. Thanks in advance!
I want to implement a listview showing android applications with their
internet usage.
An anybody suggest how should i write the code that only displays list
of application that actually use the internet
One solution (maybe only one that works best and came to my head) is to use TrafficStats class that calculating data (TCP, UDP) transferred through network. Exactly in your case, you need to get data for each UID (each application has own UID).
All what you need to know if application trasfered more that zero bytes through network and when you know that, you can tell that "this application uses network".
Here is pseudo-code you could use:
List<Application> collection = new ArrayList<Application>();
Application app = null; // some custom object is good approach
PackageManager pm = getActivity().getPackageManager();
for (ApplicationInfo info: pm.getInstalledApplications(
PackageManager.GET_META_DATA)) {
// received data by application
long downloaded = TrafficStats.getUidRxBytes(info.uid);
// transmitted data by application
long uploaded = TrafficStats.getUidTxBytes(info.uid);
// filter system applications only
if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
// check if application has network usage
if (downloaded > 0 || uploaded > 0) {
// it's application you want
}
}
// non-system application
else {
if (downloaded > 0 || uploaded > 0) {
// it's application you want
}
}
}
It's important to say that TrafficStats is available from API 8 and also Before JELLY_BEAN_MR2, this may return unsupported on devices where statistics aren't available. I used this approach and never had a problems.
Note: Also I want to mention that maybe there are another possible approach(es) for example reading from some system files but this is (at least for me) hardcoded approach and i don't recommend to use it (also in various devices files can be on different places, have different content and different filename).
I hope it will help you solve your problem.
Application use internet will need Internet Permission
You can filter out those app by checked PackageInfo.permission
first off - let me just say that I am NOT asking how to implement a share button in my app or anything like that. I know all about using Intents and Intent Filters etc etc.
what I AM asking about is this: is there any way to get access to the "Share" menu itself? in other words, I'd love to build an app that filters out some of the services I never use but that I don't want to delete from my phone completely.
I tried looking it up in the Android API, but only found info on getting your app to show up in the menu or putting a 'Share' button in your app etc.
Being that I'm still somewhat of a novice programmer, I'm also wondering if there's some way for me to sniff out the API objects that are being created/used when the 'Share' menu is built/displayed? Seems like I could do it in a Debugger session, but I'm not sure how.
Thank you in advance.
b
Well, there are two ways to go around Share menu. First one is to use
startActivity(Intent.createChooser(Intent, CharSequence)
But in this case, I am not sure how to obtain an access to the created share menu, coz it is a separate activity.
However, if you wish to have a control over the list of share items being displayed for your app, there is another way to approach your share menu item implementation.
Take a look at this code snippet:
//Prepare an intent to filter the activities you need
//Add a List<YourItemType> where you going to store the share-items
List<YourItemType> myShareList = new List<YourItemType>;
PackageManager packageManager = mContext.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
int numActivities = activities.size();
for (int i = 0; i != numActivities; ++i) {
final ResolveInfo info = activities.get(i);
String label = info.loadLabel(packageManager).toString();
//now you can check label or some other info and decide whether to add the item
//into your own list of share items
//Every item in your list should have a runnable which will execute
// proper share-action (Activity)
myShareList.add(new YourItemType(label, info.loadIcon(packageManager), new Runnable()
{
public void run() {
startResolvedActivity(intent, info);
}
}));
}
This code snippet shows how to get a list of the activities which are able to process share request. What you need to do next is to show your own UI. It is up to you what you are going to choose.
Hi I want to get a list of all of the installed applications on the users device I have been googling for the longest time but can't find what i want this link was the closest though and works fine except me being new don't understand how to use the method getPackages(); and create a list with it
http://www.androidsnippets.com/get-installed-applications-with-name-package-name-version-and-icon
Any help on how to create the actual list would be a major help i have all that code already in just can't get the list to actually show thanks for any help
I was working on something like this recently. One thing I'll say up front is to be sure and perform this in a separate thread -- querying the application information is SLOW. The following will get you a list of ALL the installed applications. This will include a lot of system apps that you probably aren't interested in.
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
To limit it to just the user-installed or updated system apps (e.g. Maps, GMail, etc), I used the following logic:
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
for(ApplicationInfo app : apps) {
//checks for flags; if flagged, check if updated system app
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
//Discard this one
//in this case, it should be a user-installed app
} else {
installedApps.add(app);
}
}
EDIT: Also, to get the name and icon for the app (which is probably what takes the longest -- I haven't done any real deep inspection on it -- use this:
String label = (String)pm.getApplicationLabel(app);
Drawable icon = pm.getApplicationIcon(app);
installedApps should have a full list of the apps you need, now. Hope this helps, but you may have to modify the logic a bit depending on what apps you need to have returned. Again, it is SLOW, but it's just something you have to work around. You might want to build a data cache in a database if it's something you'll be accessing frequently.
In my application I want to show a list of every available launcher (for homescreen) on that specific Android phone. Is it possible to get some kind of information from Android OS and how do I make this call?
Thanks!
Kind regards
Daniel
You can query the list of ResolverInfo that match with a specific Intent. The next snippet of code print all installed launchers.
PackageManager pm = getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> lst = pm.queryIntentActivities(i, 0);
for (ResolveInfo resolveInfo : lst) {
Log.d("Test", "New Launcher Found: " + resolveInfo.activityInfo.packageName);
}
The code snippet above does NOT work accurately, as the result of launchers' list also includes system's setting's app whose package name is com.android.settings. This unexpected result happens on both my Pixel 2 (Android 8.0 ) and Nexus 6 (Android 7.1).
Try the following:
Obtain the list of installed applications:
List pkgList = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);
Iterate over this list and obtain launcher activity using:
getPackageManager().getLaunchIntentForPackage(packageName);
For details read here: PackageManager. Hope this helps.
Let's say, there are four apps in the system: app1, app2, app3, app4.
Be default, when the system is up, all apps will be shown in the home screen. Now if we provide a customized log in screen, user A log in, then for this user, he can only see (and use ) app1 and app2.
Then A log out, user B log in, he can only see app3 and app4.
Does API provide such capability to load the app list dynamically?
Hope someone can help, thanks.
I think the answer depends on how you build your logging system. But, in theory,the basisc around applications list in Android system would be something like that :
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List pkgAppsList = mContext.getPackageManager().queryIntentActivities(intent, 0);