【Android】反射

來源:互聯網
上載者:User

標籤:

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】反射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.