下面列出了android.app.Application#getPackageManager ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Helper method for getting a boolean value from the apps manifest
*
* @param preference the preference key.
* @return whether the preference has been set or not
*/
public boolean isManifestSet(String preference) {
Application application = (Application) firebaseApp.getApplicationContext();
// Check if there's metadata in the manifest setting the auto-init state.
try {
PackageManager packageManager = application.getPackageManager();
if (packageManager != null) {
ApplicationInfo applicationInfo =
packageManager.getApplicationInfo(
application.getPackageName(), PackageManager.GET_META_DATA);
return applicationInfo != null
&& applicationInfo.metaData != null
&& applicationInfo.metaData.containsKey(preference);
}
} catch (PackageManager.NameNotFoundException e) {
// This shouldn't happen since it's this app's package. However, if it does, we want to fall
// through to the default, and avoid throwing an exception
}
return false;
}
/**
* Helper method for getting a boolean value from the apps stored preferences. Falls back to
* checking for a manifest preference before returning the default value.
*
* @param preference the manifest preference key.
* @param defaultValue the default value to return if the key is not found.
* @return the value stored or the default if the stored value is not found.
*/
public boolean getBooleanManifestValue(String preference, boolean defaultValue) {
Application application = (Application) firebaseApp.getApplicationContext();
// Check if there's metadata in the manifest setting the auto-init state.
try {
PackageManager packageManager = application.getPackageManager();
if (packageManager != null) {
ApplicationInfo applicationInfo =
packageManager.getApplicationInfo(
application.getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null
&& applicationInfo.metaData != null
&& applicationInfo.metaData.containsKey(preference)) {
return applicationInfo.metaData.getBoolean(preference);
}
}
} catch (PackageManager.NameNotFoundException e) {
// This shouldn't happen since it's this app's package. However, if it does, we want to fall
// through to the default, and avoid throwing an exception
}
// Return the default
return defaultValue;
}
/**
* 获取当前设备上安装的可供跨app调用组件的App列表
* @return 包名集合
*/
public static List<String> scanComponentApps() {
Application application = CC.getApplication();
String curPkg = application.getPackageName();
PackageManager pm = application.getPackageManager();
// 查询所有已经安装的应用程序
Intent intent = new Intent("action.com.billy.cc.connection");
List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
List<String> packageNames = new ArrayList<>();
for (ResolveInfo info : list) {
ActivityInfo activityInfo = info.activityInfo;
String packageName = activityInfo.packageName;
if (curPkg.equals(packageName)) {
continue;
}
if (tryWakeup(packageName)) {
packageNames.add(packageName);
}
}
return packageNames;
}
@SuppressWarnings("SameParameterValue")
private static void validateActivityInManifest(Application application, Class activityClass) throws PackageManager.NameNotFoundException {
PackageManager pm = application.getPackageManager();
String packageName = application.getPackageName();
PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
ActivityInfo[] activities = packageInfo.activities;
String activityClassName = activityClass.getName();
for (ActivityInfo activityInfo : activities) {
if (activityInfo.name.equals(activityClassName)) {
Logger.i(activityClassName.replaceFirst("com.clevertap.android.sdk.", "") + " is present");
return;
}
}
Logger.i(activityClassName.replaceFirst("com.clevertap.android.sdk.", "") + " not present");
}
public static Object createNewLoadedApk(Application application, Object obj) {
try {
Method declaredMethod;
ApplicationInfo applicationInfo = application.getPackageManager().getApplicationInfo(application.getPackageName(), 1152);
application.getPackageManager();
Resources resources = application.getResources();
if (resources instanceof DelegateResources) {
declaredMethod = resources.getClass().getSuperclass().getDeclaredMethod("getCompatibilityInfo", new Class[0]);
} else {
declaredMethod = resources.getClass().getDeclaredMethod("getCompatibilityInfo", new Class[0]);
}
declaredMethod.setAccessible(true);
Class cls = Class.forName("android.content.res.CompatibilityInfo");
Object invoke = declaredMethod.invoke(application.getResources(), new Object[0]);
Method declaredMethod2 = AtlasHacks.ActivityThread.getmClass().getDeclaredMethod("getPackageInfoNoCheck", new Class[]{ApplicationInfo.class, cls});
declaredMethod2.setAccessible(true);
invoke = declaredMethod2.invoke(obj, new Object[]{applicationInfo, invoke});
_mLoadedApk = invoke;
return invoke;
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 根据包名检查APP是否已安装
* @param context
* @param packageName
* @return
*/
public static boolean appIsInstalled(Application context, String packageName) {
PackageManager pm = context.getPackageManager();
List<PackageInfo> packageList = pm.getInstalledPackages(0);
for (PackageInfo appInfo : packageList) {
if (appInfo.packageName.equals(packageName)) {
return true;
}
}
return false;
}
public static Object createNewLoadedApk(Application application, Object obj) {
try {
Method declaredMethod;
ApplicationInfo applicationInfo = application.getPackageManager()
.getApplicationInfo(application.getPackageName(), 1152);
application.getPackageManager();
Resources resources = application.getResources();
if (resources instanceof DelegateResources) {
declaredMethod = resources
.getClass()
.getSuperclass()
.getDeclaredMethod("getCompatibilityInfo");
} else {
declaredMethod = resources.getClass().getDeclaredMethod(
"getCompatibilityInfo");
}
declaredMethod.setAccessible(true);
Class cls = Class.forName("android.content.res.CompatibilityInfo");
Object invoke = declaredMethod.invoke(application.getResources()
);
Method declaredMethod2 = OpenAtlasHacks.ActivityThread.getmClass()
.getDeclaredMethod("getPackageInfoNoCheck",
ApplicationInfo.class, cls);
declaredMethod2.setAccessible(true);
invoke = declaredMethod2.invoke(obj, applicationInfo, invoke);
_mLoadedApk = invoke;
return invoke;
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private static void validateReceiverInManifest(Application application, String receiverClassName) throws PackageManager.NameNotFoundException {
PackageManager pm = application.getPackageManager();
String packageName = application.getPackageName();
PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_RECEIVERS);
ActivityInfo[] receivers = packageInfo.receivers;
for (ActivityInfo activityInfo : receivers) {
if (activityInfo.name.equals(receiverClassName)) {
Logger.i(receiverClassName.replaceFirst("com.clevertap.android.sdk.", "") + " is present");
return;
}
}
Logger.i(receiverClassName.replaceFirst("com.clevertap.android.sdk.", "") + " not present");
}
private static void validateServiceInManifest(Application application, String serviceClassName) throws PackageManager.NameNotFoundException {
PackageManager pm = application.getPackageManager();
String packageName = application.getPackageName();
PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_SERVICES);
ServiceInfo[] services = packageInfo.services;
for (ServiceInfo serviceInfo : services) {
if (serviceInfo.name.equals(serviceClassName)) {
Logger.i(serviceClassName.replaceFirst("com.clevertap.android.sdk.", "") + " is present");
return;
}
}
Logger.i(serviceClassName.replaceFirst("com.clevertap.android.sdk.", "") + " not present");
}
private static Context getWebViewContextAndSetProvider() throws MissingWebViewPackageException {
Application initialApplication = AppGlobals.getInitialApplication();
try {
WebViewProviderResponse response = null;
Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW,
"WebViewUpdateService.waitForAndGetProvider()");
try {
response = getUpdateService().waitForAndGetProvider();
} finally {
Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
}
if (response.status != LIBLOAD_SUCCESS
&& response.status != LIBLOAD_FAILED_WAITING_FOR_RELRO) {
throw new MissingWebViewPackageException("Failed to load WebView provider: "
+ getWebViewPreparationErrorReason(response.status));
}
// Register to be killed before fetching package info - so that we will be
// killed if the package info goes out-of-date.
Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "ActivityManager.addPackageDependency()");
try {
ActivityManager.getService().addPackageDependency(
response.packageInfo.packageName);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
}
// Fetch package info and verify it against the chosen package
PackageInfo newPackageInfo = null;
PackageManager pm = initialApplication.getPackageManager();
Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "PackageManager.getPackageInfo()");
try {
newPackageInfo = pm.getPackageInfo(
response.packageInfo.packageName,
PackageManager.GET_SHARED_LIBRARY_FILES
| PackageManager.MATCH_DEBUG_TRIAGED_MISSING
// Make sure that we fetch the current provider even if its not
// installed for the current user
| PackageManager.MATCH_UNINSTALLED_PACKAGES
// Fetch signatures for verification
| PackageManager.GET_SIGNATURES
// Get meta-data for meta data flag verification
| PackageManager.GET_META_DATA);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
}
// Validate the newly fetched package info, throws MissingWebViewPackageException on
// failure
verifyPackageInfo(response.packageInfo, newPackageInfo);
ApplicationInfo ai = newPackageInfo.applicationInfo;
fixupStubApplicationInfo(ai, pm);
Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW,
"initialApplication.createApplicationContext");
try {
// Construct an app context to load the Java code into the current app.
Context webViewContext = initialApplication.createApplicationContext(
ai,
Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
sPackageInfo = newPackageInfo;
return webViewContext;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
}
} catch (RemoteException | PackageManager.NameNotFoundException e) {
throw new MissingWebViewPackageException("Failed to load WebView provider: " + e);
}
}
/**
*
* @param packageName
* @return
* @throws PackageManager.NameNotFoundException
*/
protected static Resources getResources(Application application, String packageName) throws PackageManager.NameNotFoundException {
PackageManager pm = application.getPackageManager();
return pm.getResourcesForApplication(packageName);
}