【Android】共享APK中的资源文件

1、使用相同的android:sharedUserId

2、使用下面的类

package com.lemote.ebag.Utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;

public class ResourceUtils {
	private static ResourceUtils resourceUtils = null;
	private Context mContext;
	private String mPackagename;

	private ResourceUtils() {

	}

	public static ResourceUtils getInstance() {
		if (resourceUtils == null) {
			resourceUtils = new ResourceUtils();
		}
		return resourceUtils;
	}

	public String getString(String filename) {
		if (mContext != null)
			return mContext.getResources().getString(
					mContext.getResources().getIdentifier(filename, "string",
							mContext.getPackageName()));
		return null;
	}

	public Drawable getDrawable(String filename) {
		if (mContext != null)
			return mContext.getResources().getDrawable(
					mContext.getResources().getIdentifier(filename, "drawable",
							mContext.getPackageName()));
		return null;
	}

	public boolean checkApkExist(Context context, String packageName) {
		if (packageName == null || "".equals(packageName))
			return false;
		try {
			ApplicationInfo info = context.getPackageManager()
					.getApplicationInfo(packageName,
							PackageManager.GET_UNINSTALLED_PACKAGES);
			return true;
		} catch (NameNotFoundException e) {
			return false;
		}
	}

	public void init(Context context, String packagename) {
		if (this.mPackagename != packagename) {
			try {
				mContext = context.createPackageContext(packagename,
						Context.CONTEXT_IGNORE_SECURITY);
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.mPackagename = packagename;
	}
}

3、调用方法

ResourceUtils resourceUtils = ResourceUtils.getInstance();
		resourceUtils.init(mContext, "com.lemote.about");

		icon.setImageDrawable(resourceUtils.getDrawable( "ebag"));
		tx_version.setText(resourceUtils.getString( "about_version"));
		tx_appname.setText(resourceUtils.getString( "about_appname"));
		about_company_ch.setText(resourceUtils.getString( "about_company_ch"));
		about_company_en.setText(resourceUtils.getString( "about_company_en"));
其他资源的获取  自己在工具类中添加 


你可能感兴趣的:(【Android】共享APK中的资源文件)