標籤:
package com.example.myandroidtest;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import android.R.integer;import android.app.Activity;import android.bluetooth.BluetoothAdapter;import android.content.Context;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends BaseActivity { Class<?> bclazz; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e("tag", "=MainActivity=onCreate="); try{ //擷取指定類的class對象 Class<?> clazz = Class.forName("android.app.Notification"); //clazz.newInstance(); //獲得所有建構函式 Constructor[] constructors=clazz.getConstructors(); for (Constructor constructor : constructors) { Log.e("Constructor反射建構函式:",""); //取出第i個構造方法。 System.out.print(Modifier.toString(constructor.getModifiers())); //---- 列印該構造方法的首碼修飾符 System.out.print(" "+constructor.getName()+"("); //列印該構造方法的名字 //---- 列印該構造方法的參數。 Class[] parameterTypes=constructor.getParameterTypes(); //構造方法參數集但是 數群組類型顯示特殊 for(int j=0;j<parameterTypes.length;j++) { System.out.print(parameterTypes[j].getName()); } System.out.println(")"); } //修飾符 -類名 -超類名 -介面 Log.e("Notification反射包名", Modifier.toString(clazz.getModifiers())+" " +clazz.getPackage().getName()+" extends "+clazz.getSuperclass()+" implement "+clazz.getInterfaces().getClass().getName()); //所有欄位 Field[] fields=clazz.getDeclaredFields(); //所有public欄位 //Field[] fields=clazz.getFields(); for(Field field : fields) { //數組特殊處理 if (field.getType().isArray()) { Log.e("Notification反射屬性", field.getType().getComponentType()+" "+field.getName()); } else { Log.e("Notification反射屬性", field.getType()+" "+field.getName()); } } //所有方法 Method[] methods =clazz.getDeclaredMethods(); //所有public方法 //Method[] methods =clazz.getMethods(); for(Method method : methods) { Log.e("Notification method反射name", Modifier.toString(method.getModifiers())+" "+method.getName()); } }catch (Exception e){ e.printStackTrace(); Log.e("Notification反射錯誤", "Exception"); } try{ //獲得指定包的Context對象 Context c = createPackageContext("com.example.myandroidtest", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); //獲得class對象 bclazz = Class.forName("android.bluetooth.BluetoothManager"); //所有建構函式 Constructor[] constructors=bclazz.getDeclaredConstructors(); //所有public建構函式 //Constructor[] constructors=bclazz.getConstructors(); for (Constructor constructor : constructors) { Log.e("Constructor反射建構函式:",""); //取出第i個構造方法。 System.out.print(Modifier.toString(constructor.getModifiers())); //---- 列印該構造方法的首碼修飾符 System.out.print(" "+constructor.getName()+"("); //列印該構造方法的名字 //---- 列印該構造方法的參數。 Class[] parameterTypes=constructor.getParameterTypes(); //構造方法參數集但是 數群組類型顯示特殊 for(int j=0;j<parameterTypes.length;j++) { System.out.print(parameterTypes[j].getName()); } System.out.println(")"); //constructor.newInstance(c);//執行個體化 } //有參數建構函式 Constructor con = bclazz.getConstructor(Context.class); //建立它的一個對象 //Object maObject = bclazz.newInstance();//建構函式無參數執行個體化 Log.e("calzz 是否為Null", ""+(bclazz==null)); Log.e("BluetoothManager反射包名", Modifier.toString(bclazz.getModifiers())+" " +bclazz.getPackage().getName()+" extends "+bclazz.getSuperclass()+" implement "+bclazz.getInterfaces().getClass().getName()); //所有欄位 Field[] fields=bclazz.getDeclaredFields(); for(Field field : fields) { if (field.getType().isArray()) { Log.e("BluetoothManager反射屬性", field.getType().getComponentType()+" "+field.getName()); } else { Log.e("BluetoothManager反射屬性", field.getType()+" "+field.getName()); } } //獲得所有方法 Method[] methods =bclazz.getDeclaredMethods(); for(Method method : methods) { Log.e("BluetoothManager method反射name", ""+method.getName()); if (method.getName().equals("getAdapter")) { //執行方法 method.invoke(con.newInstance(c)); } } }catch (Exception e){ e.printStackTrace(); Log.e("calzz 是否為Null", ""+(bclazz==null)); Log.e("BluetoothManager反射錯誤", "Exception"); } } }
【Android】反射