本文整理匯總了Java中android.content.Context.getClassLoader方法的典型用法代碼示例。如果您正苦於以下問題:Java Context.getClassLoader方法的具體用法?Java Context.getClassLoader怎麽用?Java Context.getClassLoader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.content.Context
的用法示例。
在下文中一共展示了Context.getClassLoader方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: fetchPluginByContext
import android.content.Context; //導入方法依賴的package包/類
private static String fetchPluginByContext(Context c, Uri uri) {
// 根據Context的ClassLoader來看到底屬於哪個插件,還是隻是主程序
ClassLoader cl = c.getClassLoader();
String pn = PluginFactory.fetchPluginName(cl);
if (TextUtils.isEmpty(pn)) {
// 獲得了無效的插件信息,這種情況很少見,故打出錯誤信息,什麽也不做
if (LOGR) {
LogRelease.e(PLUGIN_TAG, "ppc.fubc: pn is n. u=" + uri);
}
return null;
} else if (TextUtils.equals(pn, RePlugin.PLUGIN_NAME_MAIN)) {
// 此Context屬於主工程,則也什麽都不做。稍後會直接走“主程序的Context”來做處理
if (LOG) {
LogDebug.d(PLUGIN_TAG, "PluginProviderClient.fubc(): Call Main! u=" + uri);
}
return null;
} else {
// 返回這個Plugin名字
if (LOG) {
LogDebug.d(PLUGIN_TAG, "PluginProviderClient.fubc(): Call Plugin! u=" + uri);
}
return pn;
}
}
示例2: installNavitveLibraryABI
import android.content.Context; //導入方法依賴的package包/類
/**
* you can reflect your current abi to classloader library path
* as you don't need to use load*Library method above
* @param context
* @param currentABI
*/
public static void installNavitveLibraryABI(Context context, String currentABI) {
Tinker tinker = Tinker.with(context);
if (!tinker.isTinkerLoaded()) {
TinkerLog.i(TAG, "tinker is not loaded, just return");
return;
}
TinkerLoadResult loadResult = tinker.getTinkerLoadResultIfPresent();
if (loadResult.libs == null) {
TinkerLog.i(TAG, "tinker libs is null, just return");
return;
}
File soDir = new File(loadResult.libraryDirectory, "lib/" + currentABI);
if (!soDir.exists()) {
TinkerLog.e(TAG, "current libraryABI folder is not exist, path: %s", soDir.getPath());
return;
}
ClassLoader classLoader = context.getClassLoader();
if (classLoader == null) {
TinkerLog.e(TAG, "classloader is null");
return;
}
TinkerLog.i(TAG, "before hack classloader:" + classLoader.toString());
try {
installNativeLibraryPath(classLoader, soDir);
} catch (Throwable throwable) {
TinkerLog.e(TAG, "installNativeLibraryPath fail:" + throwable);
}
TinkerLog.i(TAG, "after hack classloader:" + classLoader.toString());
}
示例3: getComponentNameByContext
import android.content.Context; //導入方法依賴的package包/類
/**
* 根據Context所帶的插件信息,來填充Intent的ComponentName對象
* 若外界將Context.xxxService方法用PluginServiceClient代替(如編譯工具所做的),則這裏會做如下事情:
* 1、將外界傳遞的Intent為com.qihoo360.mobilesafe的包名,變成一個特定插件的包名
* 2、這樣交給外界的時候,其ComponentName已變成“插件名-類名”的形式,可以做下一步處理
* 3、若其後處理失敗,則仍會調用係統的相應方法(但不是該函數的職責)
*
* @param c 根據Context內容來決定如何填充Intent,此為那個Context對象
* @param from ComponentName
* @return 新的ComponentName。或者如果插件有問題,則返回from
*/
public static ComponentName getComponentNameByContext(Context c, ComponentName from) {
if (from == null) {
// 如果Intent裏麵沒有帶ComponentName,則我們不支持此特性,直接返回null
// 外界會直接調用其係統的對應方法
return null;
}
String appPackage = IPC.getPackageName();
if (!TextUtils.equals(from.getPackageName(), appPackage)) {
// 自己已填好了要使用的插件名(作為CN的Key),這裏不做處理
return from;
}
// 根據Context的ClassLoader來看到底屬於哪個插件,還是隻是主程序
ClassLoader cl = c.getClassLoader();
String pn = PluginFactory.fetchPluginName(cl);
if (TextUtils.isEmpty(pn)) {
// 獲得了無效的插件信息,這種情況很少見,故打出錯誤信息,什麽也不做
if (LOGR) {
LogRelease.e(PLUGIN_TAG, "pch.iibc: pn is n. n=" + from);
}
} else if (TextUtils.equals(pn, RePlugin.PLUGIN_NAME_MAIN)) {
// 此Context屬於主工程,則也什麽都不做。稍後會直接走“主程序的Context”來做處理
if (LOG) {
LogDebug.d(PLUGIN_TAG, "PluginClientHelper.iibc(): Call Main! n=" + from);
}
} else {
// 將Context所在的插件名寫入CN中,待後麵的方法去處理
if (LOG) {
LogDebug.d(PLUGIN_TAG, "PluginClientHelper.iibc(): Call Plugin! n=" + from);
}
return new ComponentName(pn, from.getClassName());
}
return from;
}
示例4: getJarClassLoader
import android.content.Context; //導入方法依賴的package包/類
@SuppressLint({"NewApi"})
public static JarClassLoader getJarClassLoader(Context context, String jarname, String packagename) {
String dexInternalPath = JarUtil.getJarInFolderName(context, jarname);
String optimizedDexOutputPath = JarUtil.getJarOutFolderName(context);
if (dexLoaders.containsKey(packagename)) {
return (JarClassLoader) dexLoaders.get(packagename);
}
JarClassLoader cl = new JarClassLoader(packagename, dexInternalPath, optimizedDexOutputPath, context.getApplicationInfo().nativeLibraryDir, context.getClassLoader());
dexLoaders.put(packagename, cl);
return cl;
}
示例5: loadClass
import android.content.Context; //導入方法依賴的package包/類
@SuppressLint({"NewApi"})
public static Class loadClass(Context context, String jarName, String packageName, String className) {
String dexInternalPath = JarUtil.getJarInFolderName(context, jarName);
String optimizedDexOutputPath = JarUtil.getJarOutFolderName(context);
if (!TextUtils.isEmpty(dexInternalPath)) {
try {
JarClassLoader cl;
if (dexLoaders.containsKey(packageName)) {
cl = (JarClassLoader) dexLoaders.get(packageName);
} else {
cl = new JarClassLoader(packageName, dexInternalPath, optimizedDexOutputPath, context.getApplicationInfo().nativeLibraryDir, context.getClassLoader());
dexLoaders.put(packageName, cl);
}
return cl.loadClass(packageName + "." + className);
} catch (Exception e) {
JLog.i("clf", "!!!!!!! loadClass--" + packageName + " e is " + e.getMessage());
return null;
}
} else if (mReloadNum >= 1) {
return null;
} else {
mReloadNum++;
JLog.i("plugin", "JarUtil.updatePlugin 333333333333333333");
JarUtil.updatePlugin(context, 0, false);
return loadClass(context, jarName, packageName, className);
}
}
示例6: injectDexElements
import android.content.Context; //導入方法依賴的package包/類
/**
* 合並注入
*
* @param context
* @param patchFilePath 補丁所在文件夾(盡量是SD卡的路徑, 否則會打開文件失敗)
* @throws Exception
*/
public static void injectDexElements(Context context, String patchFilePath) throws Exception {
ClassLoader pathClassLoader = context.getClassLoader();
/**dex, 優化後的路徑, 必須在要App data目錄下, 否則會沒有權限*/
File oDexFile = new File(context.getDir("odex", Context.MODE_PRIVATE).getAbsolutePath());
/**dex 補丁文件路徑(文件夾)*/
File patchFile = new File(patchFilePath);
if (!patchFile.exists()) {
patchFile.mkdirs();
}
// 合並成一個數組
Object applicationDexElement = getDexElementByClassLoader(pathClassLoader);
for (File dexFile : patchFile.listFiles()) {
ClassLoader classLoader = new DexClassLoader(dexFile.getAbsolutePath(),// dexPath
oDexFile.getAbsolutePath(),// optimizedDirectory
null,
pathClassLoader
);
// 獲取這個classLoader中的Element
Object classElement = getDexElementByClassLoader(classLoader);
//Log.e("TAG", classElement.toString());
applicationDexElement = combineArray(classElement, applicationDexElement);
}
// 注入到pathClassLoader中
injectDexElements(pathClassLoader, applicationDexElement);
}
示例7: DexLoader
import android.content.Context; //導入方法依賴的package包/類
public DexLoader(Context var1, String[] var2, String var3, String var4) {
Object var5 = var1.getClassLoader();
String var6 = var1.getApplicationInfo().nativeLibraryDir;
if(!TextUtils.isEmpty(var4)) {
var6 = var6 + File.pathSeparator + var4;
}
for(int var7 = 0; var7 < var2.length; ++var7) {
var5 = this.mClassLoader = new DexClassLoader(var2[var7], var3, var6, (ClassLoader)var5);
}
}
示例8: getProp
import android.content.Context; //導入方法依賴的package包/類
private static final String getProp(Context context, String property) {
try {
ClassLoader cl = context.getClassLoader();
Class<?> SystemProperties = cl.loadClass("android.os.SystemProperties");
Method method = SystemProperties.getMethod("get", String.class);
Object[] params = new Object[1];
params[0] = property;
return (String)method.invoke(SystemProperties, params);
} catch (Exception e) {
return null;
}
}
示例9: getServiceComponentFromIntent
import android.content.Context; //導入方法依賴的package包/類
/**
* 啟動 Service 時,從 Intent 中獲取 ComponentName
*/
private static ComponentName getServiceComponentFromIntent(Context context, Intent intent) {
ClassLoader cl = context.getClassLoader();
String plugin = PluginFactory.fetchPluginName(cl);
/* Intent 中已指定 ComponentName */
if (intent.getComponent() != null) {
// 根據 Context 所帶的插件信息,來填充 Intent 的 ComponentName 對象。具體見方法說明
return PluginClientHelper.getComponentNameByContext(context, intent.getComponent());
} else {
/* Intent 中已指定 Action,根據 action 解析出 ServiceInfo */
if (!TextUtils.isEmpty(intent.getAction())) {
ComponentList componentList = PluginFactory.queryPluginComponentList(plugin);
if (componentList != null) {
// 返回 ServiceInfo 和 Service 所在的插件
Pair<ServiceInfo, String> pair = componentList.getServiceAndPluginByIntent(context, intent);
if (pair != null) {
return new ComponentName(pair.second, pair.first.name);
}
}
} else {
if (LOG) {
LogDebug.d(PLUGIN_TAG, "PSS.startService(): No Component and no Action");
}
}
}
return null;
}
示例10: getProp
import android.content.Context; //導入方法依賴的package包/類
public static String getProp(Context context, String property) {
try {
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");
@SuppressWarnings("unchecked")
Method method = SystemProperties.getMethod("get", String.class);
Object[] params = new Object[1];
params[0] = new String(property);
return (String)method.invoke(SystemProperties, params);
} catch (Exception e) {
return null;
}
}
示例11: createClassLoader
import android.content.Context; //導入方法依賴的package包/類
public ClassLoader createClassLoader() throws TGResourceException {
Context context = this.activity.getApplicationContext();
String optimizedDirectory = context.getDir("dex", Context.MODE_PRIVATE).getAbsolutePath();
List<String> fileNames = this.unpackPlugins(optimizedDirectory);
if(!fileNames.isEmpty()) {
return new DexClassLoader(this.createPath(fileNames), optimizedDirectory, this.createLibraryPath(), context.getClassLoader());
}
return null;
}
示例12: LoadApplication
import android.content.Context; //導入方法依賴的package包/類
public static void LoadApplication (Context context, ApplicationInfo runtimePackage, String[] apks)
{
synchronized (lock) {
if (context instanceof android.app.Application) {
Context = context;
}
if (!initialized) {
android.content.IntentFilter timezoneChangedFilter = new android.content.IntentFilter (
android.content.Intent.ACTION_TIMEZONE_CHANGED
);
context.registerReceiver (new mono.android.app.NotifyTimeZoneChanges (), timezoneChangedFilter);
System.loadLibrary("monodroid");
Locale locale = Locale.getDefault ();
String language = locale.getLanguage () + "-" + locale.getCountry ();
String filesDir = context.getFilesDir ().getAbsolutePath ();
String cacheDir = context.getCacheDir ().getAbsolutePath ();
String dataDir = getNativeLibraryPath (context);
ClassLoader loader = context.getClassLoader ();
Runtime.init (
language,
apks,
getNativeLibraryPath (runtimePackage),
new String[]{
filesDir,
cacheDir,
dataDir,
},
loader,
new java.io.File (
android.os.Environment.getExternalStorageDirectory (),
"Android/data/" + context.getPackageName () + "/files/.__override__").getAbsolutePath (),
MonoPackageManager_Resources.Assemblies,
context.getPackageName ());
mono.android.app.ApplicationRegistration.registerApplications ();
initialized = true;
}
}
}
示例13: getClassLoader
import android.content.Context; //導入方法依賴的package包/類
public ClassLoader getClassLoader(ApplicationInfo appInfo) {
Context context = createPackageContext(appInfo.packageName);
return context.getClassLoader();
}
示例14: loadClassList
import android.content.Context; //導入方法依賴的package包/類
/**
* 獲取所有類
* @param context 上下文
* @param pkgName 指定要獲取類所在的包名
* @param baseClass 指定要獲取類所實現的基類
*/
public static List<Class> loadClassList(@NonNull Context context, String pkgName, @Nullable Class baseClass) {
List<Class> classList = new ArrayList<>();
Context appContext = context.getApplicationContext();
ClassLoader classLoader = appContext.getClassLoader();
String entry = "";
try {
Field pathListField = findField(classLoader, "pathList");
Object dexPathList = pathListField.get(classLoader);
Field dexElementsField = findField(dexPathList, "dexElements");
Object[] dexElements = (Object[]) dexElementsField.get(dexPathList);
for (Object dexElement : dexElements) {
Field dexFileField = findField(dexElement, "dexFile");
DexFile dexFile = (DexFile) dexFileField.get(dexElement);
Enumeration<String> entries = dexFile.entries();
while (entries.hasMoreElements()) {
entry = entries.nextElement();
if (entry.startsWith("com.android") || entry.startsWith("android")) {
// 忽略 android 包名下的類
continue;
} else if (entry.contains("$")) {
// 忽略內部類
continue;
}
if (entry.contains(pkgName)) {
Class<?> clazz = dexFile.loadClass(entry, classLoader);
if (clazz == null || clazz.isInterface()) {
// 忽略接口
continue;
}
if (baseClass != null) {
if (baseClass.isAssignableFrom(clazz)) {
classList.add(clazz);
}
} else {
classList.add(clazz);
}
}
}
}
} catch (Exception | Error e) {
Log.e(TAG, "reflect fail. class = " + entry + " Exception: " + e.getMessage());
}
return classList;
}
示例15: getClassLoader
import android.content.Context; //導入方法依賴的package包/類
/**
* Get package class loader
*
* @param context current locatorContext
* @param pkgName package name
* @return package class loader
* @throws NameNotFoundException name not found exception
*/
static ClassLoader getClassLoader(@NonNull final Context context, final String pkgName) throws NameNotFoundException
{
final Context providerContext = context.createPackageContext(pkgName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
return providerContext.getClassLoader();
}