Ext.: http://blog.csdn.net/lfdfhl/article/details/10311505
1 Packagecom.nthm.androidtestActivity;2 3 ImportJava.io.File;4 ImportJava.lang.reflect.Constructor;5 ImportJava.lang.reflect.Field;6 ImportJava.lang.reflect.Method;7 ImportCOM.NTHM.ANDROIDTEST.R;8 ImportAndroid.os.Bundle;9 Importandroid.app.Activity;Ten ImportAndroid.content.ContextWrapper; One /** A * Demo Description: - * Example of using Java Reflection technology in Android - * Classes that describe bytecode files (xxx.class) in Java are called class the * Reflection process can be considered as the process of dissecting class - */ - Public classMainactivityextendsActivity { - @Override + protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.main); A init (); at } - Private voidinit () { - Try { - /** - * 1 reflects the construction method of the non-parameter and obtains the object - * Note: in * 1 The full name of the class containing the package name should be passed in Class.forName () - * 2 newinstance () The nature of the method is to invoke the non-public construction method of the class to */ +String classname1= "Com.nthm.androidtestBean.Worker"; -Class clazz1=Class.forName (className1); theObject object1=clazz1.newinstance (); *System.out.println ("object1.tostring () =" +object1.tostring ()); $ /**Panax Notoginseng * 2 reflect the construction method with parameters and get the object - */ theString classname2= "Com.nthm.androidtestBean.Worker"; +Class clazz2=Class.forName (className2); AConstructor Constructor1=clazz2.getconstructor (int.class, String.class); theObject object2=constructor1.newinstance (18, "Xiaoming"); +System.out.println ("object2.tostring () =" +object2.tostring ()); - $ /** $ * 3 Gets the private field of the class - * Note: - * Get Common (public) fields should call the Clazz3.getfield (name) method the */ -String classname3= "Com.nthm.androidtestBean.Worker";WuyiClass clazz3=Class.forName (className3); theField Agefield1=clazz3.getdeclaredfield ("Age"); -System.out.println ("agefield1=" +ageField1); Wu - /** About * 4 getting and changing private fields of an object $ * That is, the simulate get () and set () methods - */ -String classname4= "Com.nthm.androidtestBean.Worker"; -Class clazz4=Class.forName (className4); AField Agefield2=clazz4.getdeclaredfield ("Age"); +Object object4=constructor1.newinstance (18, "Xiaoming"); the //to cancel access to a private field for the legality check -Agefield2.setaccessible (true); $ //Gets the private field of the object theObject ageobject4=Agefield2.get (OBJECT4); theSystem.out.println ("ageobject4=" +Ageobject4.getclass (). GetName ()); theSystem.out.println ("ageobject4=" +ageObject4); the - //change the value of the private field of the object again inAgefield2.set (Object4, 9527); the //re-acquired theObject ageobject5=Agefield2.get (OBJECT4); AboutSystem.out.println ("ageobject5=" +ageObject5); the the /** the * 5 Method of calling the object with parameters + */ -String classname5= "Com.nthm.androidtestBean.Worker"; theClass clazz5=Class.forName (className5);BayiMethod Method=clazz5.getmethod ("Printmessage", String.class,int.class,int.class); theObject object5=clazz5.newinstance (); theMethod.invoke (Object5, "Zhou Xing", 50,9527); - - //gets the Mbase object in the Contextwrapper object that holds the Contextimpl object theField Field=contextwrapper.class. Getdeclaredfield ("Mbase"); the //Skip Property Decoration (Public private Protect) Check theField.setaccessible (true); the //gets the value of the specified property of the object -Object Obj=field.get ( This); the //gets the other properties in the class based on the class to which the obtained value belongs theField=obj.getclass (). Getdeclaredfield ("Mpreferencesdir"); the //Skip Property Decoration (Public private Protect) Check94Field.setaccessible (true); theFile file=NewFile ("/sacard"); the //re-assigning the property the field.set (obj, file);98}Catch(Exception e) { About System.out.println (e.tostring ()); - }101 102 }103 104}
1 PackageCom.nthm.androidtestBean;2 3 Public classWorker {4 Private intAge ;5 PrivateString name;6 PublicWorker () {7 Super();8SYSTEM.OUT.PRINTLN ("---> Public Worker () {}");9 }Ten PublicWorker (intAge , String name) { One Super(); A This. Age =Age ; - This. Name =name; -SYSTEM.OUT.PRINTLN ("---> Public Worker (int age, String name) {}"); the } - Public intGetage () { - returnAge ; - } + Public voidSetage (intAge ) { - This. Age =Age ; + } A PublicString GetName () { at returnname; - } - Public voidsetName (String name) { - This. Name =name; - } - @Override in PublicString toString () { - return"Worker [age=" + Age + ", name=" + name + "]"; to } + - Public voidPrintmessage (String name,intAgeintsalary) { theSystem.out.println ("Name=" +name+ ", age=" +age+ ", salary=" +salary); * } $ Panax Notoginseng}
1 <relativelayout xmlns:android= "Http://schemas.android.com/apk/res /android " xmlns:tools=" Http://schemas.android.com/tools " 3 android:layout_width= "match_parent" 4 android:layout_height=" match _parent " 5 > 6 7 <textview 8 Android:la Yout_width= "Wrap_content" android:layout_height=" Wrap_content "10 android:text= "Use of Java reflection technology in Android" 11 Android:layout_cente Rinparent= "true"/>12
Example of using the Android reflection mechanism technology