android.content.Context.createPackageContext()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(128)

本文整理了Java中android.content.Context.createPackageContext()方法的一些代码示例,展示了Context.createPackageContext()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.createPackageContext()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:createPackageContext

Context.createPackageContext介绍

暂无

代码示例

代码示例来源:origin: oasisfeng/condom

@Override public Context createPackageContext(String packageName, int flags) throws PackageManager.NameNotFoundException {
  return mBase.createPackageContext(packageName, flags);
}

代码示例来源:origin: bumptech/glide

@NonNull
private Context findContextForPackage(Uri source, String packageName) {
 // Fast path
 if (packageName.equals(context.getPackageName())) {
  return context;
 }
 try {
  return context.createPackageContext(packageName, /*flags=*/ 0);
 } catch (NameNotFoundException e) {
  // The parent APK holds the correct context if the resource is located in a split
  if (packageName.contains(context.getPackageName())) {
   return context;
  }
  throw new IllegalArgumentException(
    "Failed to obtain context or unrecognized Uri format for: " + source, e);
 }
}

代码示例来源:origin: android-hacker/VirtualXposed

Context getAppContext(final String packageName) {
  Context context = null;
  try {
    context = getHostContext().createPackageContext(packageName,
        Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
  } catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
  }
  return context;
}

代码示例来源:origin: android-hacker/VirtualXposed

Context context;
try {
  context = mContext.createPackageContext(packageName,
      Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
  if (context == null) {

代码示例来源:origin: android-hacker/VirtualXposed

private Context createPackageContext(String packageName) {
  try {
    Context hostContext = VirtualCore.get().getContext();
    return hostContext.createPackageContext(packageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
  } catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
    VirtualRuntime.crash(new RemoteException());
  }
  throw new RuntimeException();
}

代码示例来源:origin: mikepenz/AboutLibraries

public static List<Library> detect(Context mCtx, List<Library> libraries) {
    ArrayList<Library> foundLibraries = new ArrayList<>();
    // Loop through known libraries
    for (Library library : libraries) {
      if (!TextUtils.isEmpty(library.getClassPath())) {
        try {
          Context ctx = mCtx.createPackageContext(mCtx.getPackageName(),
              Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
          Class<?> clazz = Class.forName(library.getClassPath(), false, ctx.getClassLoader());

          // Detected a library!!!
          if (clazz != null) {
            foundLibraries.add(library);
          }
        } catch (ClassNotFoundException e) {
          //e.printStackTrace();
        } catch (PackageManager.NameNotFoundException e) {
          //e.printStackTrace();
        }
      }
    }
    // Only return AppSource if app has a library
    //return libraries.size() > 0 ? new AppSource(pkg, libraries) : null;

    return foundLibraries;
  }
}

代码示例来源:origin: android-hacker/VirtualXposed

Context systemUi = context.createPackageContext(NotificationCompat.SYSTEM_UI_PKG,
    Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
int layoutId = getSystemId(systemUi, "status_bar_notification_row", "layout");

代码示例来源:origin: android-hacker/VirtualXposed

Context systemUi = context.createPackageContext(NotificationCompat.SYSTEM_UI_PKG,
    Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
int layoutId = getSystemId(systemUi, "time_axis", "layout");

代码示例来源:origin: bumptech/glide

String applicationPackageName, int iconResourceId) {
try {
 Context current = context.createPackageContext(applicationPackageName, /*flags=*/ 0);
 String resourcePackageName = current.getResources().getResourcePackageName(iconResourceId);
 return applicationPackageName.equals(resourcePackageName);

代码示例来源:origin: joyoyao/superCleanMaster

public void clearALLCache()
{
  List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);
  for (int i=0; i < packList.size(); i++)
  {
    PackageInfo packInfo = packList.get(i);
    if (  (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)
    {
      String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
      try {
        // clearing app data
        //                    Runtime runtime = Runtime.getRuntime();
        //                    runtime.exec("pm clear "+packInfo.packageName);
        Context context = getApplicationContext().createPackageContext(packInfo.packageName,Context.CONTEXT_IGNORE_SECURITY);
        deleteCache(context);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}

代码示例来源:origin: android-hacker/VirtualXposed

Context systemUi = null;
try {
  systemUi = context.createPackageContext(NotificationCompat.SYSTEM_UI_PKG, Context.CONTEXT_IGNORE_SECURITY);
} catch (PackageManager.NameNotFoundException e) {

代码示例来源:origin: bumptech/glide

@Test
public void load_withApplicationIconResourceNameUri_asDrawable_producesNonNullDrawable()
  throws ExecutionException, InterruptedException, NameNotFoundException {
 for (String packageName : getInstalledPackages()) {
  int iconResourceId = getResourceId(packageName);
  Context toUse = context.createPackageContext(packageName, /*flags=*/ 0);
  Resources resources = toUse.getResources();
  Uri uri = new Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .authority(packageName)
    .appendPath(resources.getResourceTypeName(iconResourceId))
    .appendPath(resources.getResourceEntryName(iconResourceId))
    .build();
  Drawable drawable = Glide.with(context)
    .load(uri)
    .submit()
    .get();
  assertThat(drawable).isNotNull();
 }
}

代码示例来源:origin: bumptech/glide

@Test
public void load_withApplicationIconResourceNameUri_asDrawable_withTransform_nonNullDrawable()
  throws ExecutionException, InterruptedException, NameNotFoundException {
 for (String packageName : getInstalledPackages()) {
  int iconResourceId = getResourceId(packageName);
  Context toUse = context.createPackageContext(packageName, /*flags=*/ 0);
  Resources resources = toUse.getResources();
  Uri uri = new Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .authority(packageName)
    .appendPath(resources.getResourceTypeName(iconResourceId))
    .appendPath(resources.getResourceEntryName(iconResourceId))
    .build();
  Drawable drawable = Glide.with(context)
    .load(uri)
    .apply(centerCropTransform())
    .submit()
    .get();
  assertThat(drawable).isNotNull();
 }
}

代码示例来源:origin: bumptech/glide

@Test
public void load_withApplicationIconResourceNameUri_asBitmap_producesNonNullBitmap()
  throws ExecutionException, InterruptedException, NameNotFoundException {
 for (String packageName : getInstalledPackages()) {
  int iconResourceId = getResourceId(packageName);
  Context toUse = context.createPackageContext(packageName, /*flags=*/ 0);
  Resources resources = toUse.getResources();
  Uri uri = new Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .authority(packageName)
    .appendPath(resources.getResourceTypeName(iconResourceId))
    .appendPath(resources.getResourceEntryName(iconResourceId))
    .build();
  Bitmap bitmap = Glide.with(context)
    .asBitmap()
    .load(uri)
    .submit()
    .get();
  assertThat(bitmap).isNotNull();
 }
}

代码示例来源:origin: Trumeet/MiPushFramework

@Override public Context createPackageContext(String packageName, int flags) throws PackageManager.NameNotFoundException {
  return mBase.createPackageContext(packageName, flags);
}

代码示例来源:origin: bumptech/glide

@Test
public void load_withApplicationIconResourceNameUri_asBitmap_withTransform_nonNullBitmap()
  throws ExecutionException, InterruptedException, NameNotFoundException {
 for (String packageName : getInstalledPackages()) {
  int iconResourceId = getResourceId(packageName);
  Context toUse = context.createPackageContext(packageName, /*flags=*/ 0);
  Resources resources = toUse.getResources();
  Uri uri = new Uri.Builder()
    .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
    .authority(packageName)
    .appendPath(resources.getResourceTypeName(iconResourceId))
    .appendPath(resources.getResourceEntryName(iconResourceId))
    .build();
  Bitmap bitmap = Glide.with(context)
    .asBitmap()
    .apply(centerCropTransform())
    .load(uri)
    .submit()
    .get();
  assertThat(bitmap).isNotNull();
 }
}

代码示例来源:origin: robolectric/robolectric

try {
 contextImpl =
   systemContextImpl.createPackageContext(
     applicationInfo.packageName, Context.CONTEXT_INCLUDE_CODE);
} catch (PackageManager.NameNotFoundException e) {

代码示例来源:origin: w568w/fuckView

private static String getString(int resId, Context context) throws PackageManager.NameNotFoundException {
  try {
    Context appContext = context.createPackageContext("ml.qingsu.fuckview", Context.CONTEXT_IGNORE_SECURITY);
    return appContext.getResources().getString(resId);
  } catch (NullPointerException e) {
    return null;
  }
}

代码示例来源:origin: org.robolectric/robolectric

.createPackageContext(applicationInfo.packageName, Context.CONTEXT_INCLUDE_CODE);

代码示例来源:origin: zjns/PureNeteaseCloudMusic-Xposed

static Context getPackageContext(String packageName) throws PackageManager.NameNotFoundException {
  Object currentThread = callStaticMethod(findClass("android.app.ActivityThread", null), "currentActivityThread");
  Context systemContext = (Context) callMethod(currentThread, "getSystemContext");
  return systemContext.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
}

相关文章

微信公众号

最新文章

更多

Context类方法