is package Available - Android App

Android examples for App:Package

Description

is package Available

Demo Code


//package com.java2s;
import android.content.Context;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

public class Main {

    public static boolean isAvailable(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfos = packageManager
                .getInstalledPackages(0);
        List<String> packageNames = new ArrayList<>();
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }//from   www.jav a  2  s .c o m
        }
        return packageNames.contains(packageName);
    }

    public static boolean isAvailable(Context context, File file) {
        return isAvailable(context,
                getPackageName(context, file.getAbsolutePath()));
    }

    public static String getPackageName(Context context, String filePath) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo info = packageManager.getPackageArchiveInfo(filePath,
                PackageManager.GET_ACTIVITIES);
        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;
            return appInfo.packageName; //?????????
        }
        return null;
    }
}

Related Tutorials