編輯:關於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); } }
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 } }); } }
Context.getResources().getIdentifier("activity_main", "layout", paramContext.getPackageName());
本篇小案例,完成一個倒計時。方式選擇AsyncTask。代碼貼在下面:布局文件soeasy: 接著活動代碼:package com.example.as
自定義dialog基礎版很多時候,我們在使用android sdk提供的alerdialog的時候,會因為你的系統的不同而產生不同的效果,就好比如你刷的是MIUI的系統,
1、繼承TabActivity package com.example.yqqmobilesafe; import android.app.TabActivity; i
應用進程保活基本就是圍繞兩個方面來展開:1 盡量保證進程不被殺死。2 進程被殺死後復活。細分如下:1)Service重啟2)進程守護3)Receiver觸發4)Alarm