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

java

			//访问某个属性

            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());

  

以上是 利用java反射机制,使用android系统的内部类成员函数 的全部内容, 来源链接: utcz.com/z/393399.html

回到顶部