1 import android.content.Context;
2 import android.os.Build;
3 import android.os.CustomService;
4 import android.os.ICustomService;
5 import android.os.ServiceManager;
6
7 import de.robv.android.xposed.XC_MethodHook;
8 import de.robv.android.xposed.XposedBridge;
9 import de.robv.android.xposed.XposedHelpers;
10
11 /**
12 * Created by bluce on 17/12/5.
13 */
14
15 public class SystemServiceHook extends XC_MethodHook {
16 private static CustomService oInstance;
17 private static boolean systemHooked;
18
19 @Override
20 protected void afterHookedMethod(MethodHookParam param) throws Throwable {
21 if (systemHooked) {
22 return;
23 }
24 final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
25 Class activityManagerServiceClazz = null;
26 try {
27 activityManagerServiceClazz = XposedHelpers.findClass("com.android.server.am.ActivityManagerService", classLoader);
28 } catch (RuntimeException e) {
29 // do nothing
30 }
31 if (!systemHooked && activityManagerServiceClazz!=null) {
32 systemHooked = true;
33 //系统服务启动完毕,通知自定义服务
34 XposedBridge.hookAllMethods(
35 activityManagerServiceClazz,
36 "systemReady",
37 new XC_MethodHook() {
38 @Override
39 protected final void afterHookedMethod(final MethodHookParam param) {
40 oInstance.systemReady();
41 XposedBridge.log(">>>systemReady!!!!");
42 }
43 }
44 );
45 //注册自定义服务到系统服务中
46 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
47 XposedBridge.hookAllConstructors(activityManagerServiceClazz, new XC_MethodHook() {
48 @Override
49 protected void afterHookedMethod(MethodHookParam param) throws Throwable {
50 Context context = (Context) XposedHelpers.getObjectField(param.thisObject, "mContext");
51 registerService(classLoader,context);
52 }
53 });
54 }else{
55 XposedBridge.hookAllMethods(
56 activityManagerServiceClazz,
57 "main",
58 new XC_MethodHook() {
59 @Override
60 protected final void afterHookedMethod(final MethodHookParam param) {
61 Context context = (Context) param.getResult();
62 registerService(classLoader,context);
63 }
64 }
65 );
66 }
67 }
68 }
69
70 private static void registerService(final ClassLoader classLoader, Context context) {
71 XposedBridge.log(">>>register service, Build.VERSION.SDK_INT"+Build.VERSION.SDK_INT);
72 oInstance = new CustomService(context);
73 Class<?> ServiceManager = XposedHelpers.findClass("android.os.ServiceManager",classLoader);
74 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
75 //避免java.lang.SecurityException错误,从5.0开始,selinux服务名称需要加前缀"user."
76 XposedHelpers.callStaticMethod(
77 ServiceManager,
78 "addService",
79 "user.custom.service",
80 oInstance,
81 true
82 );
83 } else {
84 XposedHelpers.callStaticMethod(
85 ServiceManager,
86 "addService",
87 "custom.service",
88 oInstance
89 );
90 }
91 }
92
93
94 //use service demo
95 public static ICustomService mService;
96 public static String someMethod(Context context) {
97 try {
98 if (mService == null) {
99 mService=ICustomService.Stub.asInterface(ServiceManager.getService("user.wx_custom.service"));
100 //mService =(ICustomService)context.getSystemService("wx_custom.service");
101 }
102 return mService.sayHello();
103 } catch (Exception e) {
104 System.out.println(e.getMessage());
105 e.printStackTrace();
106 }
107 return ">>> someMethod failed!!! ";
108 }
109 }