Android反射调用资源和id
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:41
本文介绍应用反射调用资本和id
提出问题:
app有一种叫应用墙的告白,应用墙是在你的法度榜样中弹出一个Activity来展示告白,比如豌豆广点通等,集成的时刻须要将资本经由过程复制添加到本身的项目中,然则app墙的代码是封装好的jar代码。不是源码,看不到,也不克不及修改。那么jar中的代码是若何加载本地资本的呢?
[img]http://img.blog.csdn.net/20150106193513011?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzA0NTk3MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
本身的项目中加载资本的时刻都是经由过程本项目标R文件来初始化资本,R文件是你本身的项目标R文件,和项目有关,如不雅第三方的jar文件中应用的R是来第三方SDK项目中的资本R,代码改换了项目之后铁定了是找不到你复制过来的资本的。那么这时刻就须要经由过程特别的方法来加载资本了,以便于调用在被集成的app中的资本。
经由过程原始的java反射机制的方法调用资本:
这只是一种方法,还有其他的方法。
IDHelper.java
ResourceHelper.java
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 } }); } }
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 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); } }
经由过程Android API 的经由过程反射获取id的办法
Context.getResources().getIdentifier("activity_main", "layout", paramContext.getPackageName());
应用: