Android访问第三方资源通用接口(自我整理)
2013-06-03 15:19
661 查看
下面是我自己写的本应用程序访问第三方应用程序资源的对外接口,可能会有些不足,以后在继续该进,希望对大家有所帮助。
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ThemeUtil {
static final String TAG = "ThemeUtil";
static String defalutPackageName;
public static Context createRemoteContext(Context context,
String applicationPackageName) {
Context remoteContext = null;
if (context == null || applicationPackageName == null
|| "".equals(applicationPackageName)) {
return null;
} else {
try {
remoteContext = context.createPackageContext(applicationPackageName,
Context.CONTEXT_IGNORE_SECURITY
| Context.CONTEXT_INCLUDE_CODE);
defalutPackageName = applicationPackageName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return remoteContext;
}
}
public static Resources getRemoteResources(Context remoteContext) {
Resources resources = null;
if (remoteContext != null) {
resources = remoteContext.getResources();
}
return resources;
}
public static int getResourceIdByFileName(Context remoteContext,
String fileName, String defType, String applicationPackageName) {
if (remoteContext == null || fileName == null || "".equals(fileName)
|| defType == null || "".equals(defType)
|| applicationPackageName == null || "".equals(applicationPackageName)) {
return -1;
} else {
Resources resources = getRemoteResources(remoteContext);
int resourceId = resources.getIdentifier(fileName, defType,
applicationPackageName);
return resourceId;
}
}
public static int getResourceIdByFileName(Context remoteContext,
String fileName, String defType) {
if (remoteContext == null || fileName == null || "".equals(fileName)
|| defType == null || "".equals(defType)) {
return -1;
} else {
Resources resources = getRemoteResources(remoteContext);
int resourceId = resources.getIdentifier(fileName, defType,
defalutPackageName);
return resourceId;
}
}
public static Drawable getDrawableByFileName(Context remoteContext,
String fileName) {
if (remoteContext == null || fileName == null || "".equals(fileName)) {
return null;
} else {
Drawable drawable = null;
int resourceId = getResourceIdByFileName(remoteContext, fileName,
"drawable", defalutPackageName);
Resources r = getRemoteResources(remoteContext);
drawable = r.getDrawable(resourceId);
return drawable;
}
}
public static int getColorByColorName(Context remoteContext,
String colorName, String applicationPackageName) {
if (remoteContext == null || colorName == null || "".equals(colorName)
|| applicationPackageName == null || "".equals(applicationPackageName)) {
return 0;
} else {
int resourceId = getResourceIdByFileName(remoteContext, colorName,
"color", applicationPackageName);
Resources r = getRemoteResources(remoteContext);
int color = r.getColor(resourceId);
return color;
}
}
public static View getViewByFileName(Context remoteContext,
String fileName, String applicationPackageName, ViewGroup root,
boolean attachToRoot) {
if (remoteContext == null || fileName == null || "".equals(fileName)
|| applicationPackageName == null || "".equals(applicationPackageName)) {
return null;
} else {
LayoutInflater remoteInflater = LayoutInflater.from(remoteContext);
int resourceId = getResourceIdByFileName(remoteContext, fileName,
"layout", applicationPackageName);
View view = remoteInflater.inflate(resourceId, root, attachToRoot);
return view;
}
}
public static View getViewByFileName(Context remoteContext,
String fileName, ViewGroup root, boolean attachToRoot) {
if (remoteContext == null || fileName == null || "".equals(fileName)) {
return null;
} else {
LayoutInflater remoteInflater = LayoutInflater.from(remoteContext);
int resourceId = getResourceIdByFileName(remoteContext, fileName,
"layout", defalutPackageName);
View view = remoteInflater.inflate(resourceId, root, attachToRoot);
return view;
}
}
public static View getViewByFileName(Context remoteContext,
String fileName, String applicationPackageName, ViewGroup root) {
if (remoteContext == null || fileName == null || "".equals(fileName)
|| applicationPackageName == null || "".equals(applicationPackageName)) {
return null;
} else {
LayoutInflater remoteInflater = LayoutInflater.from(remoteContext);
int resourceId = getResourceIdByFileName(remoteContext, fileName,
"layout", applicationPackageName);
View view = remoteInflater.inflate(resourceId, root);
return view;
}
}
public static View getViewByFileName(Context remoteContext,
String fileName, ViewGroup root) {
if (remoteContext == null || fileName == null || "".equals(fileName)) {
return null;
} else {
LayoutInflater remoteInflater = LayoutInflater.from(remoteContext);
int resourceId = getResourceIdByFileName(remoteContext, fileName,
"layout", defalutPackageName);
View view = remoteInflater.inflate(resourceId, root);
return view;
}
}
}
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ThemeUtil {
static final String TAG = "ThemeUtil";
static String defalutPackageName;
public static Context createRemoteContext(Context context,
String applicationPackageName) {
Context remoteContext = null;
if (context == null || applicationPackageName == null
|| "".equals(applicationPackageName)) {
return null;
} else {
try {
remoteContext = context.createPackageContext(applicationPackageName,
Context.CONTEXT_IGNORE_SECURITY
| Context.CONTEXT_INCLUDE_CODE);
defalutPackageName = applicationPackageName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return remoteContext;
}
}
public static Resources getRemoteResources(Context remoteContext) {
Resources resources = null;
if (remoteContext != null) {
resources = remoteContext.getResources();
}
return resources;
}
public static int getResourceIdByFileName(Context remoteContext,
String fileName, String defType, String applicationPackageName) {
if (remoteContext == null || fileName == null || "".equals(fileName)
|| defType == null || "".equals(defType)
|| applicationPackageName == null || "".equals(applicationPackageName)) {
return -1;
} else {
Resources resources = getRemoteResources(remoteContext);
int resourceId = resources.getIdentifier(fileName, defType,
applicationPackageName);
return resourceId;
}
}
public static int getResourceIdByFileName(Context remoteContext,
String fileName, String defType) {
if (remoteContext == null || fileName == null || "".equals(fileName)
|| defType == null || "".equals(defType)) {
return -1;
} else {
Resources resources = getRemoteResources(remoteContext);
int resourceId = resources.getIdentifier(fileName, defType,
defalutPackageName);
return resourceId;
}
}
public static Drawable getDrawableByFileName(Context remoteContext,
String fileName) {
if (remoteContext == null || fileName == null || "".equals(fileName)) {
return null;
} else {
Drawable drawable = null;
int resourceId = getResourceIdByFileName(remoteContext, fileName,
"drawable", defalutPackageName);
Resources r = getRemoteResources(remoteContext);
drawable = r.getDrawable(resourceId);
return drawable;
}
}
public static int getColorByColorName(Context remoteContext,
String colorName, String applicationPackageName) {
if (remoteContext == null || colorName == null || "".equals(colorName)
|| applicationPackageName == null || "".equals(applicationPackageName)) {
return 0;
} else {
int resourceId = getResourceIdByFileName(remoteContext, colorName,
"color", applicationPackageName);
Resources r = getRemoteResources(remoteContext);
int color = r.getColor(resourceId);
return color;
}
}
public static View getViewByFileName(Context remoteContext,
String fileName, String applicationPackageName, ViewGroup root,
boolean attachToRoot) {
if (remoteContext == null || fileName == null || "".equals(fileName)
|| applicationPackageName == null || "".equals(applicationPackageName)) {
return null;
} else {
LayoutInflater remoteInflater = LayoutInflater.from(remoteContext);
int resourceId = getResourceIdByFileName(remoteContext, fileName,
"layout", applicationPackageName);
View view = remoteInflater.inflate(resourceId, root, attachToRoot);
return view;
}
}
public static View getViewByFileName(Context remoteContext,
String fileName, ViewGroup root, boolean attachToRoot) {
if (remoteContext == null || fileName == null || "".equals(fileName)) {
return null;
} else {
LayoutInflater remoteInflater = LayoutInflater.from(remoteContext);
int resourceId = getResourceIdByFileName(remoteContext, fileName,
"layout", defalutPackageName);
View view = remoteInflater.inflate(resourceId, root, attachToRoot);
return view;
}
}
public static View getViewByFileName(Context remoteContext,
String fileName, String applicationPackageName, ViewGroup root) {
if (remoteContext == null || fileName == null || "".equals(fileName)
|| applicationPackageName == null || "".equals(applicationPackageName)) {
return null;
} else {
LayoutInflater remoteInflater = LayoutInflater.from(remoteContext);
int resourceId = getResourceIdByFileName(remoteContext, fileName,
"layout", applicationPackageName);
View view = remoteInflater.inflate(resourceId, root);
return view;
}
}
public static View getViewByFileName(Context remoteContext,
String fileName, ViewGroup root) {
if (remoteContext == null || fileName == null || "".equals(fileName)) {
return null;
} else {
LayoutInflater remoteInflater = LayoutInflater.from(remoteContext);
int resourceId = getResourceIdByFileName(remoteContext, fileName,
"layout", defalutPackageName);
View view = remoteInflater.inflate(resourceId, root);
return view;
}
}
}
相关文章推荐
- 第三部分:Android 应用程序接口指南---第三节:应用程序资源---第二章 资源访问
- 超全!整理常用的iOS第三方资源
- Android---52---使用URl访问网络资源
- [转]【开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发
- Android 对于第三方预置可卸载的apk,如何客制化其在主题中的资源?
- GitHub第三方资源库整理(OC篇)
- 开源中国-android软件资源整理
- Android06_Android资源访问
- 转载:Android相关网络资源整理
- Android资源代码 源码 整理 Github开源项目下载地址
- 整理常用的iOS第三方资源
- android学习—— 通过包名访问其他apk下的资源
- android 开发工具,资源,组件,风格整理集合
- Unity接入多个SDK的通用接口开发与资源管理(三)
- 在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口【转】
- Android中workerthread与UIthread同时访问同一资源造成ConcurrentModificationException解决参考
- SSAS : 数据访问接口整理汇总
- 超全!整理常用的iOS第三方资源
- Android 资源组织和访问
- 在Android中访问WebService接口的方法