本文整理匯總了Java中android.content.pm.PackageManager.getResourcesForActivity方法的典型用法代碼示例。如果您正苦於以下問題:Java PackageManager.getResourcesForActivity方法的具體用法?Java PackageManager.getResourcesForActivity怎麽用?Java PackageManager.getResourcesForActivity使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.content.pm.PackageManager
的用法示例。
在下文中一共展示了PackageManager.getResourcesForActivity方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: replaceTargetDrawablesIfPresent
import android.content.pm.PackageManager; //導入方法依賴的package包/類
/**
* Searches the given package for a resource to use to replace the Drawable on the
* target with the given resource id
* @param component of the .apk that contains the resource
* @param name of the metadata in the .apk
* @param existingResId the resource id of the target to search for
* @return true if found in the given package and replaced at least one target Drawables
*/
public boolean replaceTargetDrawablesIfPresent(ComponentName component, String name,
int existingResId) {
if (existingResId == 0) return false;
boolean replaced = false;
if (component != null) {
try {
PackageManager packageManager = getContext().getPackageManager();
// Look for the search icon specified in the activity meta-data
Bundle metaData = packageManager.getActivityInfo(
component, PackageManager.GET_META_DATA).metaData;
if (metaData != null) {
int iconResId = metaData.getInt(name);
if (iconResId != 0) {
Resources res = packageManager.getResourcesForActivity(component);
replaced = replaceTargetDrawables(res, existingResId, iconResId);
}
}
} catch (NameNotFoundException e) {
Log.w(THIS_FILE, "Failed to swap drawable; "
+ component.flattenToShortString() + " not found", e);
} catch (Resources.NotFoundException nfe) {
Log.w(THIS_FILE, "Failed to swap drawable from "
+ component.flattenToShortString(), nfe);
}
}
if (!replaced) {
// Restore the original drawable
replaceTargetDrawables(getContext().getResources(), existingResId, existingResId);
}
return replaced;
}
示例2: get
import android.content.pm.PackageManager; //導入方法依賴的package包/類
@Nullable
@ColorInt
public static Integer get(Context context, ComponentName componentName) {
PackageManager packageManager = context.getPackageManager();
ActivityInfo activityInfo = null;
PackageInfo packageInfo = null;
Resources resources = null, activityResources = null;
try {
packageInfo = packageManager.getPackageInfo(componentName.getPackageName(), PackageManager.GET_META_DATA);
resources = packageManager.getResourcesForApplication(packageInfo.applicationInfo);
activityInfo = packageManager.getActivityInfo(componentName, 0);
activityResources = packageManager.getResourcesForActivity(componentName);
} catch (PackageManager.NameNotFoundException ignored) {
}
if (packageInfo != null && resources != null) {
if (activityInfo != null && activityResources != null) {
List<Integer> activityStatusBarColors = getResourceColors(activityInfo.packageName, resources, activityInfo.theme);
if (activityStatusBarColors.size() > 0) {
return activityStatusBarColors.get(0);
}
}
List<Integer> statusBarColors = getResourceColors(packageInfo.packageName, resources, packageInfo.applicationInfo.theme);
if (statusBarColors.size() > 0) {
return statusBarColors.get(0);
}
if (packageInfo.activities != null) {
for (ActivityInfo otherActivityInfo : packageInfo.activities) {
List<Integer> otherStatusBarColors = getResourceColors(packageInfo.packageName, resources, otherActivityInfo.theme);
if (otherStatusBarColors.size() > 0) {
return otherStatusBarColors.get(0);
}
}
}
}
return null;
}