下面列出了android.content.pm.PackageManager#NameNotFoundException() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void sendMail(@NonNull Context ctx) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "[email protected]", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, ctx.getString(R.string.appName) + " (com.metinkaler.prayer)");
String versionCode = "Undefined";
String versionName = "Undefined";
try {
versionCode = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0).versionCode + "";
versionName = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0).versionName + "";
} catch (PackageManager.NameNotFoundException e) {
Crashlytics.logException(e);
}
emailIntent.putExtra(Intent.EXTRA_TEXT,
"===Device Information===" +
"\nUUID: " + Preferences.UUID.get() +
"\nManufacturer: " + Build.MANUFACTURER +
"\nModel: " + Build.MODEL +
"\nAndroid Version: " + Build.VERSION.RELEASE +
"\nApp Version Name: " + versionName +
"\nApp Version Code: " + versionCode +
"\n======================\n\n");
ctx.startActivity(Intent.createChooser(emailIntent, ctx.getString(R.string.mail)));
}
@Override
public ApplicationInfo getApplicationInfo() {
//这里的ApplicationInfo是从LoadedApk中取出来的
//由于目前插件之间是共用1个插件进程。LoadedApk只有1个,而ApplicationInfo每个插件都有一个,
// 所以不能通过直接修改loadedApk中的内容来修正这个方法的返回值,而是将修正的过程放在Context中去做,
//避免多个插件之间造成干扰
if (mApplicationInfo == null) {
try {
mApplicationInfo = getPackageManager().getApplicationInfo(mPluginDescriptor.getPackageName(), 0);
//这里修正packageManager中hook时设置的插件packageName
mApplicationInfo.packageName = getPackageName();
} catch (PackageManager.NameNotFoundException e) {
LogUtil.printException("PluginContextTheme.getApplicationInfo", e);
}
}
return mApplicationInfo;
}
public void openApp() {
String thePackage = (String) jsonObject.opt("package");
try {
Intent intent = manager.getLaunchIntentForPackage(thePackage);
if (intent != null) {
context.startActivity(intent);
} else {
throw new PackageManager.NameNotFoundException();
}
} catch (PackageManager.NameNotFoundException e) {
MainActivity.showMessage("This program is not available on your device", (Activity)context);
}
}
@Nullable
public static PackageMeta forPackage(Context context, String packageName) {
try {
PackageManager pm = context.getPackageManager();
ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);
PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
return new PackageMeta.Builder(applicationInfo.packageName)
.setLabel(applicationInfo.loadLabel(pm).toString())
.setHasSplits(applicationInfo.splitPublicSourceDirs != null && applicationInfo.splitPublicSourceDirs.length > 0)
.setIsSystemApp((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)
.setVersionCode(Utils.apiIsAtLeast(Build.VERSION_CODES.P) ? packageInfo.getLongVersionCode() : packageInfo.versionCode)
.setVersionName(packageInfo.versionName)
.setIcon(applicationInfo.icon)
.setInstallTime(packageInfo.firstInstallTime)
.setUpdateTime(packageInfo.lastUpdateTime)
.build();
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}
/**
* Get all the app signatures for the current package
* @return
*/
public ArrayList<String> getAppSignatures() {
ArrayList<String> appCodes = new ArrayList<>();
try {
// Get all package signatures for the current package
String packageName = getPackageName();
PackageManager packageManager = getPackageManager();
Signature[] signatures = packageManager.getPackageInfo(packageName,
PackageManager.GET_SIGNATURES).signatures;
// For each signature create a compatible hash
for (Signature signature : signatures) {
String hash = hash(packageName, signature.toCharsString());
if (hash != null) {
appCodes.add(String.format("%s", hash));
}
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Unable to find package to obtain hash.", e);
}
return appCodes;
}
/**
* 获取versionCode
*
* @param context cox
* @return versionCode
*/
public static int getVersionCode(Context context) {
int versionCode = 0;
try {
versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
private static String getCurrentVersionName(@NonNull final Context context) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName + (App.isProVersion() ? " Pro" : "");
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return "Unkown";
}
public Drawable getFullResIcon(ActivityInfo info) {
Resources resources;
try {
resources = mPackageManager.getResourcesForApplication(info.applicationInfo);
} catch (PackageManager.NameNotFoundException e) {
resources = null;
}
if (resources != null) {
int iconId = info.getIconResource();
if (iconId != 0) {
return getFullResIcon(resources, iconId);
}
}
return getFullResDefaultActivityIcon();
}
/**
* Checks with its package name, if an application is a system app, or is the app
* is installed as a system app.
*
* @param packageManager PackageManager object used to receive application info.
* @param componentName Application package name to check against.
*
* @return boolean True if the application is a system app, false if otherwise.
*/
public static boolean isSystemApp(PackageManager packageManager, String componentName) {
try {
ApplicationInfo appFlags = packageManager.getApplicationInfo(
getPackageName(componentName), 0);
if ((appFlags.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
return true;
}
} catch (PackageManager.NameNotFoundException e) {
Utils.sendLog(Utils.LogLevel.ERROR, e.toString());
return false;
}
return false;
}
/**
* @return The app name if successful, otherwise packagename
*/
private String getAppName(PackageManager pm, String packageName) {
String appName;
try {
appName = (String) pm.getApplicationLabel(pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
appName = packageName;
}
return appName;
}
private static PackageInfo getPackageInfo() {
PackageInfo info = null;
try {
PackageManager manager = App.getApplication().getPackageManager();
info = manager.getPackageInfo(App.getApplication().getPackageName(), PackageManager.GET_CONFIGURATIONS);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return info;
}
/**
* 判断是否存在pckName包
*/
public static boolean isPackageExist(Context context, String pckName) {
try {
PackageInfo pckInfo = context.getPackageManager()
.getPackageInfo(pckName, 0);
if (pckInfo != null)
return true;
} catch (PackageManager.NameNotFoundException ignored) {
}
return false;
}
public static String getVersionName(Activity activity) {
// 获取packagemanager的实例
PackageManager packageManager = activity.getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo = null;
try {
packInfo = packageManager.getPackageInfo(activity.getPackageName(), 0);
String version = packInfo.versionName;
return version;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "0";
}
}
/**
* Loads an icon from the icon pack based on the received package name.
*
* @param activity where LauncherApps service can be retrieved.
* @param appPackageName Package name of the app whose icon is to be loaded.
*
* @return Drawable Will return null if there is no icon associated with the package name,
* otherwise an associated icon from the icon pack will be returned.
*/
private static Drawable getIconDrawable(Activity activity, String appPackageName, long user) {
PackageManager packageManager = activity.getPackageManager();
String componentName = "ComponentInfo{" + appPackageName + "}";
Resources iconRes = null;
Drawable defaultIcon = null;
try {
if (Utils.atLeastLollipop()) {
LauncherApps launcher = (LauncherApps) activity.getSystemService(
Context.LAUNCHER_APPS_SERVICE);
UserManager userManager = (UserManager) activity.getSystemService(
Context.USER_SERVICE);
if (userManager != null && launcher != null) {
defaultIcon = launcher.getActivityList(AppUtils.getPackageName(appPackageName),
userManager.getUserForSerialNumber(user))
.get(0).getBadgedIcon(0);
}
} else {
defaultIcon = packageManager.getActivityIcon(
ComponentName.unflattenFromString(appPackageName));
}
if (!"default".equals(iconPackageName)) {
iconRes = packageManager.getResourcesForApplication(iconPackageName);
} else {
return defaultIcon;
}
} catch (PackageManager.NameNotFoundException e) {
Utils.sendLog(Utils.LogLevel.ERROR, e.toString());
}
String drawable = mPackagesDrawables.get(componentName);
if (drawable != null && iconRes != null) {
return loadDrawable(iconRes, drawable, iconPackageName);
} else {
return defaultIcon;
}
}
/**
* Check if the user has the Google TTS Engine installed
*
* @param ctx the application context
* @return true if the Google TTS Engine is installed
*/
public static boolean isGoogleTTSAvailable(final Context ctx) {
try {
ctx.getApplicationContext().getPackageManager().getApplicationInfo(TTSDefaults.TTS_PKG_NAME_GOOGLE, 0);
return true;
} catch (final PackageManager.NameNotFoundException e) {
return false;
}
}
@Test
public void testAuthorize_authorizeInProgress() throws PackageManager.NameNotFoundException {
final Activity mockActivity = mock(Activity.class);
TestUtils.setupNoSSOAppInstalled(mockActivity);
when(mockAuthState.isAuthorizeInProgress()).thenReturn(true);
// Verify that when authorize is in progress, callback is notified of error.
authClient.authorize(mockActivity, mockCallback);
verify(mockCallback).failure(any(TwitterAuthException.class));
}
public static String getSelfVersionName(Context context) {
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return pInfo.versionName;
} catch (PackageManager.NameNotFoundException ex) {
return ex.toString();
}
}
private boolean versionCheck(String packageName) {
try {
int myVersion =
Versions.getScalarApiVersion(context.getPackageName(), context.getResources());
int packageVersion =
Versions.getScalarApiVersion(
packageName, context.getPackageManager().getResourcesForApplication(packageName));
return versionCheck(myVersion, packageVersion);
} catch (PackageManager.NameNotFoundException e) {
if (Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, "Can't resolve package " + packageName, e);
}
return false;
}
}
/**
* 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.
*/
@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 taskProviderClassName = TaskContentProvider.class.getName();
ComponentName componentName = new ComponentName(packageName, taskProviderClassName);
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 = packageName;
/* Make sure that the registered authority matches the authority from the Contract */
String incorrectAuthority =
"Error: TaskContentProvider registered with authority: " + actualAuthority +
" instead of expected authority: " + expectedAuthority;
assertEquals(incorrectAuthority,
actualAuthority,
expectedAuthority);
} catch (PackageManager.NameNotFoundException e) {
String providerNotRegisteredAtAll =
"Error: TaskContentProvider 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);
}
}
/**
* Gets a {@code float} from the meta data specified in the
* AndroidManifest.xml.
*
* @param context the application context.
* @param name the android:name value
* @param defaultValue if the value doesn't exist the defaultValue will be
* used.
* @return meta data value specified in the AndroidManifest.xml if exists;
* otherwise defaultValue.
*/
public static float getMetaData(Context context, String name, float defaultValue) {
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appi = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
return appi.metaData.getFloat(name, defaultValue);
} catch (PackageManager.NameNotFoundException ex) {
Log.w(TAG, "<meta-data android:name=\"" + name + "\" ... \\> not found");
return defaultValue;
}
}