문제

For all the installed apps on a phone, I need to access the:

  • name
  • icon
  • google play url from where the app can be installed

I've looked at ApplicationInfo but that doesn't seem to provide this information to me. I've heard of PackageInfo and ResolveInfo, however I am quite confused what is the difference between them and which one should be used where? Lastly, what can I use to find the above three details about the installed apps ?

For package names, I get things like com.something.some

This is the code I am using to get apps:

final PackageManager pm = getPackageManager();
ArrayList<ApplicationData> listAppData = new ArrayList<ApplicationData>();

//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {
    listAppData.add(new ApplicationData(packageInfo));
}

ApplicationData is my own class where I extract information from packageInfo

도움이 되었습니까?

해결책

This function will return a list of apps with icon, name and package.

With the package you can build the url with:

https://play.google.com/store/apps/details?id= <PACKAGE>

See:

public class Application {

    private String packageName;
    private String name;
    private Drawable icon;
    private boolean checked;
...

private List<Application> getAllApplications(Context context)
            throws NameNotFoundException {

        PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packages = packageManager.getInstalledPackages(0);

        List<Application> myPackages = new ArrayList<Application>();

        for (PackageInfo pack : packages) {
            if (pack.versionName == null) {
                continue;
            }

            Application newPack = new Application();
            newPack.setPackageName(pack.packageName);
            newPack.setName(pack.applicationInfo.loadLabel(packageManager)
                    .toString());
            newPack.setIcon(pack.applicationInfo.loadIcon(packageManager));

            myPackages.add(newPack);
        }

        return myPackages;
    }

다른 팁

which one should be used where?

These structures represent results returned by different queries. Such structures and collections of such structures are the only way Java can return multiple data for one function call. So you use them when you make queries that return them.

I recommend to search stackoverflow for examples of the usage of these structures (that is, the search string will be "stackoverflow structure android"). One such example: Android launch browser without specifying a URL

One more snippet:

//Drawable d; Button b;
d = getPackageManager().getApplicationIcon(pkg);
b.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);

and one more:

 public boolean isPackageInstalled(String targetPackage){
     PackageManager pm=getPackageManager();
     try {
         PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
     } catch (NameNotFoundException e) {
         return false;
     }  
     return true;
}

in simple terms

These classes points to same source AndroidManifest.xml of an Application. The difference is in terms of Access Level nothing more than that. Sometimes you need all the information but most of the times you need to deal with specific information only. In those cases there is no need to parse all the content of the manifest file.

PackageInfo has access to all the content of the Manifest. This can be used if you want to read every detail of Manifest

ApplicationInfo has access to Application Tag of Manifest file thus ignoring all the irrelevant area of the file.

ResolveInfo has access to intent-filters thus ignoring all the other details.

ApplicationInfo and ResolveInfo are just handy classes that we can use according to our requirement.

From the docs and some examples on how to use them:

ApplicationInfo documentation:

Information you can retrieve about a particular application. This corresponds to information collected from the AndroidManifest.xml's tag.

Example:

ApplicationInfo.FLAG_SYSTEM tag to filter out System Apps

if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
{
// system app
}

PackageInfo documentation:

Overall information about the contents of a package. This corresponds to all of the information collected from AndroidManifest.xml.

Example: To get versionCode of the app

long versionCode = 0L;
if (Build.VERSION.SDK_INT >= VERSION_CODES.P) {
        versionCode = packageManager.getPackageInfo(packageName, 0).getLongVersionCode();
} else {
        versionCode = (Long) packageManager.getPackageInfo(packageName, 0).getVersionCode();
}

ResolveInfo documentation:

Information that is returned from resolving an intent against an IntentFilter. This partially corresponds to information collected from the AndroidManifest.xml's tags.

Example:

final Intent newIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( newIntent, 0);
for (ResolveInfo rInfo : list) {
System.out.println("Installed Applications supporting category launcher " + rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
}

The above example will help you to get ResolveInfo to start a application

PackageManager pm = this.getPackageManager();

Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>) 
    pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo : list) {
    System.out.println("Installed Applications " + rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top