利用java反射机制,使用android系统的内部类成员函数

//访问某个属性
Context mmsCtx = createPackageContext("com.android.lockscreen"
        Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); 
Class<?> maClass = Class.forName("com.android.lockscreen.LockScreenActivity", true, mmsCtx.getClassLoader()); 
 
Object maObject = maClass.newInstance();
Field f_mPm = maClass.getDeclaredField("TAG"); 
f_mPm.setAccessible(true);             
    Log.i(LOG_TAG, "___________________________________________obj:"+f_mPm.get(maObject));

  

//访问方法

         
//访问
try {
            Context mmsCtx = createPackageContext("com.android.lockscreen"
                    Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); 
            Class<?> maClass = Class.forName("com.android.lockscreen.LockScreenActivity", true, mmsCtx.getClassLoader()); 
             
            Object maObject = maClass.newInstance();
            Field f_mPm = maClass.getDeclaredField("TAG"); 
             
            //maClass.getDeclaredMethod("", parameterTypes);
            f_mPm.setAccessible(true);             
            Log.i(LOG_TAG, "___________________________________________obj:"+f_mPm.get(maObject));
                 
             
                Class[] resource = new Class[]{Context.class};
                Object[] resourceObj = new Object[]{mmsCtx};
                 
                Method addsMethod = maClass.getDeclaredMethod("getLockScreenView", resource);
                addsMethod.setAccessible(true);
                 
                View addsResult = (View)addsMethod.invoke(maObject, resourceObj);
                Log.i(LOG_TAG, "___________________________________________addsResult:"+addsResult);
                 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
//定义被访问
 
public class LockScreenActivity extends Activity {
         
    private String TAG = "LockScreenActivity";
    //private LockScreen mScreen = new LockScreen(LockScreenActivity.this);
     
    private LockScreen getLockScreenView(Context context){
         
        Log.i(TAG, "_________________________________________context:"+context);
        return new LockScreen(context);
    }
     
}

  

//获取setting包的的Context 
            Context mmsCtx = createPackageContext("com.android.settings"
                    Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); 
            //使用setting的classloader加载com.android.settings.ManageApplications类 
            Class<?> maClass = Class.forName("com.android.settings.ManageApplications", true, mmsCtx.getClassLoader()); 
            //创建它的一个对象 
            Object maObject = maClass.newInstance(); 
               
            /*
             * 将私有域mPm赋值。因为mPm在SizeObserver的invokeGetSize中用到了,
             * 却因为没有执行onCreate而没有初始化,所以要在此处初始化。
             */ 
            Field f_mPm = maClass.getDeclaredField("mPm"); 
            f_mPm.setAccessible(true);             
            f_mPm.set(maObject, mmsCtx.getPackageManager()); 
              

  

分类: android solve

本文转自wanqi博客园博客,原文链接http://www.cnblogs.com/wanqieddy/archive/2011/10/25/2224200.html:如需转载请自行联系原作者

上一篇:JavaWeb之tomcat安装、配置与使用(一)


下一篇:win10电脑桌面便签怎么固定在桌面?