Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 反射調用資源和id

Android 反射調用資源和id

編輯:關於Android編程

本文介紹利用反射調用資源和id

提出問題:

app有一種叫應用牆的廣告,應用牆是在你的程序中彈出一個Activity來展示廣告,比如豌豆廣點通等,集成的時候需要將資源通過復制添加到自己的項目中,但是app牆的代碼是封裝好的jar代碼。不是源碼,看不到,也不能修改。那麼jar中的代碼是如何加載本地資源的呢?

vcC0vNPU2NfK1LTBy6Os0tSx49PatffTw9Tasbu8r7PJtcRhcHDW0LXE18rUtKGjPC9wPgo8cD48YnI+CjwvcD4KPHA+zai5/dStyry1xGphdmG3tMnku/rWxrXEt73KvbX308PXytS0o7o8L3A+CjxwPtXi1rvKx9K71ta3vcq9o6y7udPQxuTL+7XEt73KvaGjPC9wPgo8cD5JREhlbHBlci5qYXZhPC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">public class IDHelper { public static int getLayout(Context mContext, String layoutName) { return ResourceHelper.getInstance(mContext).getLayoutId(layoutName); } public static int getViewID(Context mContext, String IDName) { return ResourceHelper.getInstance(mContext).getId(IDName); } public static int getDrawable(Context context, String drawableName) { return ResourceHelper.getInstance(context).getDrawableId(drawableName); } public static int getAttr(Context context, String attrName) { return ResourceHelper.getInstance(context).getAttrId(attrName); } public static int getString(Context context, String stringName) { return ResourceHelper.getInstance(context).getStringId(stringName); } }


ResourceHelper.java

public class ResourceHelper {
	private static ResourceHelper mResource = null;
	private static String mPackagename = null;
	private static Class mLayout = null;
	private static Class mDrawable = null;
	private static Class mID = null;
	private static Class mString = null;
	private static Class mAttr = null;

	public static ResourceHelper getInstance(Context context) {
		if (mResource == null) {
			mPackagename = (mPackagename == null ? context.getPackageName()
					: mPackagename);
			mResource = new ResourceHelper(mPackagename);
		}
		return mResource;
	}

	public ResourceHelper(String packageName) {
		try {
			mLayout = Class.forName(packageName + ".R$layout");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			mDrawable = Class.forName(packageName + ".R$drawable");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			mID = Class.forName(packageName + ".R$id");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			mString = Class.forName(packageName + ".R$string");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			mAttr = Class.forName(packageName + ".R$attr");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

	private int getResourceId(Class classType, String resourceName) {
		if (classType == null) {
			throw new IllegalArgumentException(
					"ResClass is not initialized. Please make sure you have added neccessary resources. Also make sure you have "
							+ mPackagename
							+ ".R$* configured in obfuscation. field="
							+ resourceName);
		}
		try {
			Field field = classType.getField(resourceName);
			return field.getInt(resourceName);
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("ResourceHelper",
					"Error getting resource. Make sure you have copied all resources (res/) from SDK to your project.");
		}
		return -1;
	}

	//
	public int getDrawableId(String resourceName) {
		return getResourceId(mDrawable, resourceName);
	}

	public int getLayoutId(String resourceName) {
		return getResourceId(mLayout, resourceName);
	}

	public int getId(String resourceName) {
		return getResourceId(mID, resourceName);
	}

	public int getStringId(String resourceName) {
		return getResourceId(mString, resourceName);
	}

	public int getAttrId(String resourceName) {
		return getResourceId(mAttr, resourceName);
	}

}

使用:

使用的時候不需要通過R來調用資源

public class MainActivity extends ActionBarActivity {
	private Button mButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(IDHelper.getLayout(getApplicationContext(),
				"activity_main"));//字符串是layout文件的名字
		initView();
	}

	private void initView() {
		mButton = (Button) findViewById(IDHelper.getViewID(
				getApplicationContext(), "button1"));
		mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(getApplicationContext(), "HelloWorld",
						Toast.LENGTH_LONG).show();//字符串是控件的id
			}
		});
	}
}



通過Android API 的通過反射獲取id的方法

Context.getResources().getIdentifier("activity_main", "layout", 
                paramContext.getPackageName()); 



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved