How to get exactly "Unknown Sources" application list on Android? - android

How to get exactly "Unknown Sources" application list on Android?
what is flag to used? If it is not this method,what are others method? and
How to programmically?
PackageManager pm = this.getPackageManager();
final List<PackageInfo> appinstalled = pm
.getInstalledPackages(what is flag??); //
Sorry for my bad English.
Thank you.

Someone else may have a better solution, but here's the best I could come up with...
Iterate through all installed packages, comparing each one against a known "good" package that has a good signature, with checkSignatures(int, int) or checkSignatures(String, String). If the return value of checkSignatures is SIGNATURE_UNKNOWN_PACKAGE, then you've got a package from an unknown source.
Otherwise, it doesn't look like any of the flags to getInstalledPackages are intended to filter for unknown sources.

I know this is an old post but I think the following answer might help you:
https://stackoverflow.com/a/42248268/2212770
public static List<String> getAppsFromUnknownSources(Context context)
{
List<String> apps = new ArrayList<>();
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> packList = packageManager.getInstalledPackages(0);
for (int i = 0; i < packList.size(); i++)
{
PackageInfo packInfo = packList.get(i);
boolean hasEmptyInstallerPackageName = packageManager.
getInstallerPackageName(packageInfo.packageName) == null;
boolean isUserInstalledApp = (packageInfo.applicationInfo.flags &
ApplicationInfo.FLAG_SYSTEM) == 0;
if (hasEmptyInstallerPackageName && isUserInstalledApp)
{
apps.add(packInfo.packageName);
}
}
return apps;
}

Related

Android get list of non Play Store apps

As a safety measure I would like to get the list of apps that aren't installed from the Play Store. Is there a way to do this?
The packageManager contains a method getInstalledApplications but I don't know which flags to add to get the list. Any help would be appreciated.
Edit: Here is an code example of v4_adi's answer.
public static List<String> getAppsFromUnknownSources(Context context)
{
List<String> apps = new ArrayList<>();
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> packList = packageManager.getInstalledPackages(0);
for (int i = 0; i < packList.size(); i++)
{
PackageInfo packInfo = packList.get(i);
if (packageManager.getInstallerPackageName(packInfo.packageName) == null)
{
apps.add(packInfo.packageName);
}
}
return apps;
}
This is a good start, however this also returns a lot off pre-installed Android and Samsung apps. Is there anyway to remove them from the list? I only want user installed apps from unknown sources.
The following link has answer to your question
The PackageManager class supplies the getInstallerPackageName method that will tell you the package name of whatever installed the package you specify. Side-loaded apps will not contain a value.
How to know an application is installed from google play or side-load?
Originally I thought it would be enough to retrieve the apps that weren't installed via the Google Play Store. Later I found that I also needed to filter out the pre-installed system applications.
I found the last part of the puzzle in another post: Get list of Non System Applications
public static List<String> getAppsFromUnknownSources(Context context)
{
List<String> apps = new ArrayList<>();
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> packList = packageManager.getInstalledPackages(0);
for (int i = 0; i < packList.size(); i++)
{
PackageInfo packInfo = packList.get(i);
boolean hasEmptyInstallerPackageName = packageManager
.getInstallerPackageName(packageInfo.packageName) == null;
boolean isUserInstalledApp = (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0;
if (hasEmptyInstallerPackageName && isUserInstalledApp)
{
apps.add(packInfo.packageName);
}
}
return apps;
}

How know apps that have permission to access internet

I make an app that need to know which application in device have permission to access to internet .
Anyone can help me?
( I searched my question but I found nothing.)
you can check it this way
PackageManager p = getPackageManager();
final List<PackageInfo> appinstall = p.getInstalledPackages(PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS);
for (int i=0; i < appinstall.size(); i++)
{
PackageInfo packageInfo = appinstall.get(i);
PermissionInfo[] permissions = packageInfo.permissions;
// permissions The array of all the permissions
}
Hope this will solve your problem...
I found the best solution (thanx for Kushal answer)
This is a function that give list of apps that have permission of access to internet
private List<PackageInfo> getAppsWithInternetPermission(){
List<PackageInfo> appsThatHaveInternetPermissin = new ArrayList<PackageInfo>();
PackageManager p = getPackageManager();
final List<PackageInfo> appinstall = p.getInstalledPackages(PackageManager.GET_PERMISSIONS);
for (int i=0; i < appinstall.size(); i++)
{
PackageInfo packageInfo = appinstall.get(i);
permissions = packageInfo.requestedPermissions;
try{
for (String permissinName : permissions) {
if(permissinName.equals("android.permission.INTERNET")){
appsThatHaveInternetPermissin.add(packageInfo);
}
}
} catch(NullPointerException e){
Log.i("catch messege", e + "");
}
}
return appsThatHaveInternetPermissin;
}

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.

Filter out non-launchable apps when getting all installed apps

Im working on a app where I want to present the user with all installed apps and let him/her choose one and then do something with it. I followed a tutorial (this: http://impressive-artworx.de/2011/list-all-installed-apps-in-style/ ) although I'm having some issues. After following the tutorial I only got apps that weren't preinstalled (like all background apps that aren't launchable) which is great if you want the apps that the user has downloaded from the play store. The problem is that in my app I want to display the launchable system apps like Youtube and Browser but not the non-launchable ones like Search Application Provider.
Here's the code that I'm using when to get the apps:
private List<App> loadInstalledApps(boolean includeSysApps) {
List<App> apps = new ArrayList<App>();
// the package manager contains the information about all installed apps
PackageManager packageManager = getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0); //PackageManager.GET_META_DATA
for(int i=0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
App app = new App();
app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
app.setPackageName(p.packageName);
app.setVersionName(p.versionName);
app.setVersionCode(p.versionCode);
CharSequence description = p.applicationInfo.loadDescription(packageManager);
app.setDescription(description != null ? description.toString() : "");
apps.add(app);
}
return apps;
}
Now my question is; what is the best way to filter out the non-launchable apps?
Any help is appreciated!
The Best way is:
public static List<ApplicationInfo> getAllInstalledApplications(Context context) {
List<ApplicationInfo> installedApps = context.getPackageManager().getInstalledApplications(PackageManager.PERMISSION_GRANTED);
List<ApplicationInfo> launchableInstalledApps = new ArrayList<ApplicationInfo>();
for(int i =0; i<installedApps.size(); i++){
if(context.getPackageManager().getLaunchIntentForPackage(installedApps.get(i).packageName) != null){
//If you're here, then this is a launch-able app
launchableInstalledApps.add(installedApps.get(i));
}
}
return launchableInstalledApps;
}

Installed application is third-party or not

How can I get the list of installed third-party applications on Android phone.
I am able to get the list of application with the code below but I want only third-party applications.
PackageManager pm = context.getPackageManager();
appInstalModel.setAppName(p.applicationInfo.loadLabel(context.getPackageManager()).toString());
appInstalModel.setAppPkg(p.packageName);
appInstalModel.setAppVersionName(p.versionName);
List<ApplicationInfo> apps = getPackageManager().getInstalledApplications(0);
for (int i=0; i < apps.size(); i++)
{
if ((apps.get(i).flags & ApplicationInfo.FLAG_SYSTEM) == 1)
{
//System app
}
}
RoflcoptrException's answer is correct. But in some cases it won't give you all the installed third-party applications. ApplicationInfo also has flag FLAG_UPDATED_SYSTEM_APP which is set
If this application has been install as an update to a built-in system
application
On my smart phone such applications include Amazone Kindle, Adobe Reader, Slacker Radio and others. These applications did not come with the phone and were installed from Google Play Store. Thus, they can be considered as third-party apps.
So, you may also want to check FLAG_UPDATED_SYSTEM_APP flag.
final PackageManager packageManager = _context.getPackageManager();
List<ApplicationInfo> installedApplications =
packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo appInfo : installedApplications)
{
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)
{
// IS A SYSTEM APP
}
if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0)
{
// APP WAS INSTALL AS AN UPDATE TO A BUILD-IN SYSTEM APP
}
}
The ApplicationInfo object will have the FLAG_SYSTEM flag unset. The sdmove program might have some sample code.
small changes in #Roflcoptr answer.
List<ApplicationInfo> apps = getPackageManager().getInstalledApplications(0);
for (int i=0; i < apps.size(); i++)
{
if ((apps.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
{
//System app
}
}
Thanks #Roflcoptr for your answer.
public static List<PackageInfo> getInstalledAppList(Context context) {
ArrayList<PackageInfo> packList = (ArrayList<PackageInfo>) context.getPackageManager().getInstalledPackages(0);
showLog("/n/n ********************** App List ********************");
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(context.getPackageManager()).toString();
showLog(appName + "(" + packInfo.packageName + ")");
} else {
packList.remove(i);
i--;
}
}
showLog("List Size : " + packList.size());
showLog("/n/n ********************** END ********************");
return packList;
}

Categories

Resources