Android访问第三方资源通用接口(自我整理)

下面是我自己写的本应用程序访问第三方应用程序资源的对外接口,可能会有些不足,以后在继续该进,希望对大家有所帮助。
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;
  }
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值