前言
记录一些反射使用情况
使用反射判断一个类的是否继承指定接口类
接口类
public interface DemoService { void run(); }
实现类
public class DemoImport implements DemoService{ @Override public void run() { Log.e("调试_临时_log", "this_run"); } }
判断代码
private void hasInterfaces() { Class<?> d = DemoImport.class; Class<?>[] interfacesArray = d.getInterfaces();//获取这个类的所以接口类数组 for (Class<?> item : interfacesArray) { if (item == DemoService.class) { //判断是否有继承的接口 Log.e("调试_临时_log", "this_true"); } } }
注解,反射实例,反射调用方法
/* 扫描特定包下的所有带有WORKANNOTATION的注解类,实例化后注册,如果work需要特别的实例化,需要实现静态函数creat。 必须在子线程调用,否则会阻塞主线程 */ public static void launch(Context c, String[] packageNameList) { if(null == packageNameList || packageNameList.length == 0){ throw new IllegalArgumentException("WorkService launch packageNameList is empty !"); } L.dd(TAG, "launch begin = " + System.currentTimeMillis()); for(int i =0; i<packageNameList.length; i++) { List<String> allClassStr = ClassUtil.getClassName(c, packageNameList[i]); for (int j = 0; j < allClassStr.size(); j++) { String classStr = allClassStr.get(j); //L.dd(TAG, "launch begin classStr = " + classStr); Class<?> className = null; try { className = Class.forName(classStr); } catch (ClassNotFoundException e) { e.printStackTrace(); } if (null == className) { L.ww(TAG, "Class = " + classStr + " no found !"); continue; } WORKANNOTATION classAnn = className.getAnnotation(WORKANNOTATION.class); if (classAnn == null) { continue; } Method createMethod = null; try { createMethod = className.getMethod("create"); }catch (Exception e){ e.printStackTrace(); } IWork work = null; //如果没有创建的函数,则实例化无参数的构造函数 if(null == createMethod){ L.ee(TAG, "Class = " + classStr + " has no create method !"); try { work = (IWork)className.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); L.ee(TAG, "Class = " + classStr + " newInstance throw InstantiationException !"); } catch (IllegalAccessException e) { e.printStackTrace(); L.ee(TAG, "Class = " + classStr + " newInstance throw IllegalAccessException !"); }; }else{ try { work = (IWork) createMethod.invoke(null); }catch (IllegalAccessException e){ e.printStackTrace(); L.ee(TAG, "Class = " + classStr + " create throw IllegalAccessException !"); }catch (InvocationTargetException e){ e.printStackTrace(); L.ee(TAG, "Class = " + classStr + " create throw InvocationTargetException !"); } } if(null == work){ L.ee(TAG, "Class = " + classStr + " create fail !"); continue; } L.d("Work className = " + className.toString() + " register sucess ! "); WorkService.register(work); } } L.dd(TAG, "launch end = " + System.currentTimeMillis()); Intent intent = new Intent(c, WorkService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { c.startForegroundService(intent); } else { c.startService(intent); } }
反射Android系统AIDL服务
fun demo1(){ try { val getServiceMethod: Method = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String::class.java) val windowManager = getServiceMethod.invoke(null, "window") val cStub = Class.forName("android.view.IWindowManager\$Stub") val asInterface: Method = cStub.getMethod("asInterface", IBinder::class.java) val iWindowManager = asInterface.invoke(null, windowManager) val updateRotation = iWindowManager.javaClass.getMethod("updateRotation", Boolean::class.java,Boolean::class.java) Log.e("zh", "onCreate: windowManager ${iWindowManager}" ) //registerPointerEventListener val methods = iWindowManager.javaClass.methods // val interfaces = iWindowManager.javaClass.interfaces val superclass = iWindowManager.javaClass.methods superclass.forEach { Log.e("zh", "onCreate: interfaces = ${it.name}" ) } // val setforcedDisplaySize = iWindowManager.javaClass.getMethod("setForcedDisplaySize", Int::class.javaPrimitiveType, Int::class.javaPrimitiveType, Int::class.javaPrimitiveType) // val clearForcedDisplaySize = iWindowManager.javaClass.getMethod("clearForcedDisplaySize", Int::class.javaPrimitiveType) // Log.e("zh", "onCreate:${setforcedDisplaySize} ") // Log.e("zh", "onCreate:${clearForcedDisplaySize} ") } catch (e: NoSuchMethodException) { e.printStackTrace() Log.e("zh", "无此方法例外" ) } catch (e: ClassNotFoundException) { e.printStackTrace() Log.e("zh", "类未找到异常" ) } catch (e: IllegalAccessException) { e.printStackTrace() Log.e("zh", "非法访问例外" ) } catch (e: InvocationTargetException) { e.printStackTrace() Log.e("zh", "调用目标异常" ) } }
End
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/14030419.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。