您的位置:首页 > 移动开发 > Android开发

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;

  }

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android