Java Code Examples for android.content.pm.PackageManager.NameNotFoundException

The following are Jave code examples for showing how to use NameNotFoundException of the android.content.pm.PackageManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: RLibrary   File: Device.java   View Source Code Vote up 6 votes
public static String appVersionCode(Context context) {
    PackageManager pm = context.getPackageManager();
    PackageInfo pi = null;
    try {
        pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return "unknown";
    }
    return pi.versionCode + "";
}
 
Example 2
Project: SimpleUILauncher   File: IconCache.java   View Source Code Vote up 6 votes
/**
 * Pre-load an icon into the persistent cache.
 *
 * <P>Queries for a component that does not exist in the package manager
 * will be answered by the persistent cache.
 *
 * @param componentName the icon should be returned for this component
 * @param icon the icon to be persisted
 * @param dpi the native density of the icon
 */
public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
        long userSerial, InvariantDeviceProfile idp) {
    // TODO rescale to the correct native DPI
    try {
        PackageManager packageManager = mContext.getPackageManager();
        packageManager.getActivityIcon(componentName);
        // component is present on the system already, do nothing
        return;
    } catch (PackageManager.NameNotFoundException e) {
        // pass
    }

    icon = Bitmap.createScaledBitmap(icon, idp.iconBitmapSize, idp.iconBitmapSize, true);
    Bitmap lowResIcon = generateLowResIcon(icon, Color.TRANSPARENT);
    ContentValues values = newContentValues(icon, lowResIcon, label,
            componentName.getPackageName());
    values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
    values.put(IconDB.COLUMN_USER, userSerial);
    mIconDb.insertOrReplace(values);
}
 
Example 3
Project: RLibrary   File: RUtils.java   View Source Code Vote up 6 votes
/**
     * 返回app的版本代码.
     *
     * @param context the context
     * @return app version code
     */
    public static int getAppVersionCode(Context context) {
// 获取package manager的实例
        PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
        int code = 1;
        PackageInfo packInfo;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(),
                    0);
            code = packInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
// Log.i("版本代码:", version);
        return code;
    }
 
Example 4
Project: NotificationsApp   File: MainActivity.java   View Source Code Vote up 5 votes
private boolean isAppInstalled(String packageName) {
    PackageManager pm = getPackageManager();
    boolean installed = false;
    try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        installed = false;
    }
    return installed;
}
 
Example 5
Project: GitHub   File: Kits.java   View Source Code Vote up 5 votes
/**
 * 获取当前版本
 *
 * @param context
 * @return
 */
public static String getVersionName(Context context) {
    PackageManager pManager = context.getPackageManager();
    PackageInfo packageInfo = null;
    try {
        packageInfo = pManager.getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return packageInfo.versionName;
}
 
Example 6
Project: mvparms   File: DeviceUtils.java   View Source Code Vote up 5 votes
/**
 * 当前的包是否存在
 *
 * @param context
 * @param pckName
 * @return
 */
public static boolean isPackageExist(Context context, String pckName) {
    try {
        PackageInfo pckInfo = context.getPackageManager()
                .getPackageInfo(pckName, 0);
        if (pckInfo != null)
            return true;
    } catch (PackageManager.NameNotFoundException e) {
        Log.e("TDvice", e.getMessage());
    }
    return false;
}
 
Example 7
Project: Brevent   File: BreventActivity.java   View Source Code Vote up 5 votes
private boolean isFrameworkPackage(PackageManager packageManager, String packageName) {
    if (fakeFramework) {
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
            return isFrameworkPackage(packageManager, packageInfo);
        } catch (PackageManager.NameNotFoundException e) {
            UILog.w("cannot find " + packageName, e);
            return false;
        }
    } else {
        return packageManager.checkSignatures(PACKAGE_FRAMEWORK, packageName) ==
                PackageManager.SIGNATURE_MATCH;
    }
}
 
Example 8
Project: react-native-udesk   File: UdeskUtil.java   View Source Code Vote up 5 votes
public static String getAppName(Context context) {
    String appName = "";
    try {
        PackageManager manager = context.getPackageManager();
        PackageInfo info = null;
        info = manager.getPackageInfo(context.getPackageName(), 0);
        appName = info.applicationInfo.loadLabel(manager).toString();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return appName;
}
 
Example 9
Project: lineagex86   File: NotificationStation.java   View Source Code Vote up 5 votes
private CharSequence loadPackageName(String pkg) {
    try {
        ApplicationInfo info = mPm.getApplicationInfo(pkg,
                PackageManager.GET_UNINSTALLED_PACKAGES);
        if (info != null) return mPm.getApplicationLabel(info);
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Cannot load package name", e);
    }
    return pkg;
}
 
Example 10
Project: Brevent   File: OpsItemAdapter.java   View Source Code Vote up 5 votes
private PermissionGroupInfo getPermissionGroupInfo(PackageManager packageManager, String group) {
    if (TextUtils.isEmpty(group)) {
        return null;
    }
    try {
        return packageManager.getPermissionGroupInfo(group, 0);
    } catch (PackageManager.NameNotFoundException e) {
        UILog.w("Can't find permission group " + group, e);
        return null;
    }
}
 
Example 11
Project: FlickLauncher   File: IconPackManager.java   View Source Code Vote up 5 votes
public HashMap<String, IconPack> getAvailableIconPacks(boolean forceReload)
{
    if (iconPacks == null || forceReload)
    {
        iconPacks = new HashMap<String, IconPack>();

        // find apps with intent-filter "com.gau.go.launcherex.theme" and return build the HashMap
        PackageManager pm = mContext.getPackageManager();

        List<ResolveInfo> adwlauncherthemes = pm.queryIntentActivities(new Intent("org.adw.launcher.THEMES"), PackageManager.GET_META_DATA);
        List<ResolveInfo> golauncherthemes = pm.queryIntentActivities(new Intent("com.gau.go.launcherex.theme"), PackageManager.GET_META_DATA);

        // merge those lists
        List<ResolveInfo> rinfo = new ArrayList<ResolveInfo>(adwlauncherthemes);
        rinfo.addAll(golauncherthemes);

        for(ResolveInfo ri  : rinfo)
        {
            IconPack ip = new IconPack();
            ip.packageName = ri.activityInfo.packageName;

            ApplicationInfo ai = null;
            try
            {
                ai = pm.getApplicationInfo(ip.packageName, PackageManager.GET_META_DATA);
                ip.name  = mContext.getPackageManager().getApplicationLabel(ai).toString();
                iconPacks.put(ip.packageName, ip);
            }
            catch (PackageManager.NameNotFoundException e)
            {
                // shouldn't happen
                e.printStackTrace();
            }
        }
    }
    return iconPacks;
}
 
Example 12
Project: androidtools   File: AppUtils.java   View Source Code Vote up 5 votes
/**
 * Read the meta-data information under the application tag
 *
 * @param context context
 * @return Bundle information for metaData under application tag [Bundle]
 */
public static Bundle getMetaDaApplication(Context context) {
    try {
        ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
        return applicationInfo.metaData;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 13
Project: Utils   File: AppUtil.java   View Source Code Vote up 5 votes
/**
 * 判断App是否是系统应用
 *
 * @param context     上下文
 * @param packageName 包名
 * @return {@code true}: 是<br>{@code false}: 否
 */
public static boolean isSystemApp(Context context, String packageName) {
    if (DataUtil.isNullString(packageName))
        return false;
    try {
        PackageManager pm = context.getPackageManager();
        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
        return ai != null && (ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 14
Project: linkopensdk-android   File: LinkApiImplV3.java   View Source Code Vote up 5 votes
@Override
public boolean isLinkInstalled(Context context, String packageName) {
    PackageInfo packageInfo;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        packageInfo = null;
        e.printStackTrace();
    }
    if (packageInfo == null) {
        return false;
    } else {
        return true;
    }
}
 
Example 15
Project: ESSocialSDK-master   File: SocialUtils.java   View Source Code Vote up 5 votes
/**
 * 是否安装了该包名的app
 *
 * @param context context
 * @param pkgName 包名
 * @return 是否安装
 */
private static boolean isPackageInstalled(Context context, String pkgName) {
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo pkgInfo = pm.getPackageInfo(pkgName, 0);
        return pkgInfo != null && pkgInfo.applicationInfo.enabled;
    } catch (PackageManager.NameNotFoundException e) {
    }
    return false;
}
 
Example 16
Project: MaterialAbout   File: AboutBuilder.java   View Source Code Vote up 5 votes
/**
 * Displays the app version below the app name
 *
 * @return the same {@link AboutBuilder} instance
 */
public AboutBuilder setVersionNameAsAppSubTitle() {
    try {
        return setAppTitle(context.getString(com.vansuita.materialabout.R.string.version, getPackageInfo().versionName));
    } catch (PackageManager.NameNotFoundException e) {
        return setAppTitle(R.string.error);
    }
}
 
Example 17
Project: javaide   File: SysUtils.java   View Source Code Vote up 5 votes
public static String getVersionName(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        L.e(e);
        return null;
    }
}
 
Example 18
Project: android-dev-challenge   File: TestWeatherProvider.java   View Source Code Vote up 4 votes
/**
 * This test checks to make sure that the content provider is registered correctly in the
 * AndroidManifest file. If it fails, you should check the AndroidManifest to see if you've
 * added a <provider/> tag and that you've properly specified the android:authorities attribute.
 * <p>
 * Potential causes for failure:
 * <p>
 *   1) Your WeatherProvider was registered with the incorrect authority
 * <p>
 *   2) Your WeatherProvider was not registered at all
 */
@Test
public void testProviderRegistry() {

    /*
     * A ComponentName is an identifier for a specific application component, such as an
     * Activity, ContentProvider, BroadcastReceiver, or a Service.
     *
     * Two pieces of information are required to identify a component: the package (a String)
     * it exists in, and the class (a String) name inside of that package.
     *
     * We will use the ComponentName for our ContentProvider class to ask the system
     * information about the ContentProvider, specifically, the authority under which it is
     * registered.
     */
    String packageName = mContext.getPackageName();
    String weatherProviderClassName = WeatherProvider.class.getName();
    ComponentName componentName = new ComponentName(packageName, weatherProviderClassName);

    try {

        /*
         * Get a reference to the package manager. The package manager allows us to access
         * information about packages installed on a particular device. In this case, we're
         * going to use it to get some information about our ContentProvider under test.
         */
        PackageManager pm = mContext.getPackageManager();

        /* The ProviderInfo will contain the authority, which is what we want to test */
        ProviderInfo providerInfo = pm.getProviderInfo(componentName, 0);
        String actualAuthority = providerInfo.authority;
        String expectedAuthority = WeatherContract.CONTENT_AUTHORITY;

        /* Make sure that the registered authority matches the authority from the Contract */
        String incorrectAuthority =
                "Error: WeatherProvider registered with authority: " + actualAuthority +
                        " instead of expected authority: " + expectedAuthority;
        assertEquals(incorrectAuthority,
                actualAuthority,
                expectedAuthority);

    } catch (PackageManager.NameNotFoundException e) {
        String providerNotRegisteredAtAll =
                "Error: WeatherProvider not registered at " + mContext.getPackageName();
        /*
         * This exception is thrown if the ContentProvider hasn't been registered with the
         * manifest at all. If this is the case, you need to double check your
         * AndroidManifest file
         */
        fail(providerNotRegisteredAtAll);
    }
}
 
Example 19
Project: container   File: ComponentFixer.java   View Source Code Vote up 4 votes
public static void fixApplicationInfo(AppSetting setting, ApplicationInfo applicationInfo, int userId) {
	applicationInfo.flags |= ApplicationInfo.FLAG_HAS_CODE;
	if (TextUtils.isEmpty(applicationInfo.processName)) {
		applicationInfo.processName = applicationInfo.packageName;
	}
	applicationInfo.name = fixComponentClassName(setting.packageName, applicationInfo.name);
	applicationInfo.publicSourceDir = setting.apkPath;
	applicationInfo.sourceDir = setting.apkPath;
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
		applicationInfo.splitSourceDirs = new String[]{setting.apkPath};
		applicationInfo.splitPublicSourceDirs = applicationInfo.splitSourceDirs;
		ApplicationInfoL.scanSourceDir.set(applicationInfo, applicationInfo.dataDir);
		ApplicationInfoL.scanPublicSourceDir.set(applicationInfo, applicationInfo.dataDir);
           String hostPrimaryCpuAbi = ApplicationInfoL.primaryCpuAbi.get(VirtualCore.get().getContext().getApplicationInfo());
		ApplicationInfoL.primaryCpuAbi.set(applicationInfo, hostPrimaryCpuAbi);
	}
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
		ApplicationInfoN.deviceEncryptedDataDir.set(applicationInfo, applicationInfo.dataDir);
		ApplicationInfoN.deviceProtectedDataDir.set(applicationInfo, applicationInfo.dataDir);
		ApplicationInfoN.credentialEncryptedDataDir.set(applicationInfo, applicationInfo.dataDir);
		ApplicationInfoN.credentialProtectedDataDir.set(applicationInfo, applicationInfo.dataDir);
	}
	applicationInfo.enabled = true;
	applicationInfo.nativeLibraryDir = setting.libPath;
	applicationInfo.dataDir = VEnvironment.getDataUserPackageDirectory(userId, setting.packageName).getPath();
	applicationInfo.uid = setting.appId;
	if (setting.dependSystem) {
		String[] sharedLibraryFiles = sSharedLibCache.get(setting.packageName);
		if (sharedLibraryFiles == null) {
			PackageManager hostPM = VirtualCore.get().getUnHookPackageManager();
			try {
				ApplicationInfo hostInfo = hostPM.getApplicationInfo(setting.packageName, PackageManager.GET_SHARED_LIBRARY_FILES);
				sharedLibraryFiles = hostInfo.sharedLibraryFiles;
				if (sharedLibraryFiles == null) sharedLibraryFiles = new String[0];
				sSharedLibCache.put(setting.packageName, sharedLibraryFiles);
			} catch (PackageManager.NameNotFoundException e) {
				e.printStackTrace();
			}
		}
		applicationInfo.sharedLibraryFiles = sharedLibraryFiles;
	}
}
 
Example 20
Project: MiPushFramework   File: PseudoContextWrapper.java   View Source Code Vote up 4 votes
@RequiresApi(O) @Override public Context createContextForSplit(final String splitName) throws PackageManager.NameNotFoundException {
	return mBase.createContextForSplit(splitName);
}