Android reflection call resource and id

Source: Internet
Author: User

Android reflection call resource and id

This topic describes how to call resources and id Using Reflection.

Raise questions:

An app has an advertisement called an app wall. An app wall pops up an Activity in your app to display the advertisement, for example, petabytes of text, during integration, You need to copy the resources to your project, but the app wall code is encapsulated jar code. It is not the source code. It cannot be seen or modified. How does the Code in jar load local resources?

Authorization + CjwvcD4KPHA + zai5/dStyry1xGphdmG3tMnku/authorization + authorization = "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);}}

Usage:

You do not need to use R to call resources.

Public class MainActivity extends ActionBarActivity {private Button mButton; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (IDHelper. getLayout (getApplicationContext (), "activity_main"); // the string is the name of the layout file initView ();} private void initView () {mButton = (Button) findViewById (IDHelper. getViewID (getApplicationContext (), "button1"); mButton. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {Toast. makeText (getApplicationContext (), "HelloWorld", Toast. LENGTH_LONG ). show (); // the string is the Control id }});}}



How to obtain the id through reflection through the Android API

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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.