1.通过反射技术可以访问到其他包名下数据方法等,这些为一些APK换皮肤提供了方便
首先初始化skinContext
1
2
3
4
5
6
7
8
|
try
{
skinContext =
this
.createPackageContext(
"com.skin"
,
CONTEXT_IGNORE_SECURITY|CONTEXT_INCLUDE_CODE);
}
catch
(NameNotFoundException e) {
// TODO Auto-generated catch block
skinContext=
null
;
e.printStackTrace();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
/**
* 取得对应包的所有资源的ID
* 存在MAP中
* @param packageName
* @return
*/
private
Map
{
Map
Map
try
{
//取得皮肤包中的R文件
Class rClass = skinContext.getClassLoader().loadClass(packageName+
".R"
);
//取得记录各种资源的ID的类
Class[] resClass =rClass.getClasses();
String className,resourceName;
int
resourceId=
0
;
for
(
int
i=
0
;i
temp.put(resourceName, resourceId);
Log.i(
"DDDDD"
,
"className:"
+className+
" resourceName:"
+resourceName+
" "
+
"resourceId:"
+Integer.toHexString(resourceId));
}
}
//由于内部类的关系className应该是com.skin.R$layout的形式
//截掉前面的包名和.R$以方便使用
className = className.substring(packageName.length()+
3
);
resMap.put(className, temp);
}
}
catch
(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
resMap;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
/**
* 获取皮肤包中的layout
* 并转化为VIEW
* @param layoutName
* @return
*/
private
View getLayoutFromSkin(String layoutName)
{
View view;
if
(resMap ==
null
)
return
null
;
Map
int
viewId = (Integer) temp.get(layoutName);
if
(viewId !=
0
)
{
//引用皮肤包资源转化View
LayoutInflater inflater =LayoutInflater.from(skinContext);
view = inflater.inflate(skinContext.getResources().getLayout(viewId),
null
);
}
else
{
view =
null
;
}
return
view;
}
|
2. 访问android 隐藏的API
Toast信息框的关闭是由系统管理的,因为hide方法是隐藏的开发者没有办法直接调用,这种情况下可以用发射机制获取这个方法,创建一个显示和隐藏都由开发者控制的Toast信息框。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package
com.example.reflection;
import
java.lang.reflect.Field;
import
java.lang.reflect.Method;
import
android.content.Context;
import
android.util.Log;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
MyToast
{
Context context=
null
;
Object obj =
null
;
public
MyToast(Context context,String text)
{
this
.context =context;
Toast toast =Toast.makeText(context, text,
1
);
try
{
Field field = toast.getClass().getDeclaredField(
"mTN"
);
field.setAccessible(
true
);
obj =field.get(toast);
}
catch
(Exception e) {
// TODO: handle exception
Log.d(
"AAA"
,
"MyToast Exception--->"
+e.toString());
}
}
public
void
show()
{
try
{
//android4.0以上就要以下处理
// Field mNextViewField = obj.getClass().getDeclaredField("mNextView");
// mNextViewField.setAccessible(true);
// LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// View v = inflate.inflate(R.layout.ui_toast, null);
// mNextViewField.set(obj, v);
Method method =obj.getClass().getDeclaredMethod(
"show"
,
null
);
method.invoke(obj,
null
);
}
catch
(Exception e) {
// TODO Auto-generated catch block
Log.d(
"AAA"
,
"show Exception--->"
+e.toString());
e.printStackTrace();
}
}
public
void
hide()
{
try
{
Method method =obj.getClass().getDeclaredMethod(
"hide"
,
null
);
method.invoke(obj,
null
);
}
catch
(Exception e) {
// TODO Auto-generated catch block
Log.d(
"AAA"
,
"hide Exception--->"
+e.toString());
e.printStackTrace();
}
}
}
|
1
2
|
MyToast toast =
new
MyToast(
this
,
"反射机制!"
);
toast.show();
|
toast.hide();
注意在4.0以上的版本中,还需要对Toast 中的View进行处理,如代码中所示
3. 修改某些“不可改” 的系统资源
ListView组件没有提供修改快速滑块图像的API,因此不能直接修改,但可通过反射实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package
com.example.reflection;
import
java.lang.reflect.Field;
import
android.content.Context;
import
android.graphics.drawable.Drawable;
import
android.util.AttributeSet;
import
android.widget.AbsListView;
import
android.widget.ListView;
public
class
MListView
extends
ListView
{
public
MListView(Context context, AttributeSet attrs)
{
super
(context, attrs);
// TODO Auto-generated constructor stub
setNewDrawable(context);
}
private
void
setNewDrawable(Context context)
{
try
{
Field field = AbsListView.
class
.getDeclaredField(
"mFastScroller"
);
field.setAccessible(
true
);
Object obj = field.get(
this
);
field =field.getType().getDeclaredField(
"mThumbDrawable"
);
field.setAccessible(
true
);
Drawable drawable = (Drawable)field.get(obj);
drawable = context.getResources().getDrawable(R.drawable.ic_launcher);
field.set(obj, drawable);
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
1
|
Field field = AbsListView.
class
.getDeclaredField(
"mFastScroller"
);
|
1
2
|
|
1
|
android:fastScrollEnabled=
"true"
|
效果图如下:
往sim卡里面复制短信
访问API隐藏的方法:往sim卡里面复制短信的时候,需要用到方法
copyMessageToIcc
,但是在apk中是hide的,那么我们就需要用反射的方法来获取出这个方法了:// smsManager.copyMessageToIcc(null, pdu, 1);
public void insert() {
SmsManager smsManager = SmsManager.getDefault();
byte[] smsc = new byte[2];
smsc[0] = 0x1;
smsc[1] = 0x2;
String clxs = "123";
byte[] pdu = hexStringToBytes("200d91683119603514f90000313021023025230331d90c");
Class> c;
try {
c = Class.forName("android.telephony.SmsManager");
Method[] ms = c.getMethods();
for (Method m : ms) {
System.out.println(m.getName());
Class>[] cx = m.getParameterTypes();
for (Class> cx1 : cx)
System.out.println(cx1.getName());
System.out.println(m.getReturnType());
}
Method m = c.getMethod("copyMessageToIcc", new Class[] {
byte[].class, byte[].class, int.class });
Object s = m.invoke(SmsManager.getDefault(), null, pdu, 1);
} catch (Exception e) {
e.printStackTrace();
}
// smsManager.copyMessageToIcc(null, pdu, 1);
}
总结:
Java中的反射机制,被称为Reflection,它允许运行中的Java程序对自身进行检查,并能直接操作程序的内部属性或方法。Reflection机制允许程序在正在执行的过程中,利用Reflection APIs取得任何已知名称的类的内部信息,包括:package、 type parameters、 superclass、 implemented interfaces、 inner classes、 outer classes、 fields、 construct等
MainActivity如下: