使用反射机制调用buid中的函数接口

来源:互联网 发布:i menu安装软件 编辑:程序博客网 时间:2024/05/01 12:03
package com.xx.xx;


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;




import android.os.Build;
public class Hitv_IC {
private static String TAG = "Hitv_IC";


public Hitv_IC()
{
}

public static String getBuildMethod(String function) throws Exception
{
String values = null;
Class buildClass = Class.forName("android.os.Build");
    Constructor constructor = buildClass.getConstructor();
    Object mbuildClass = constructor.newInstance();
    Method invokeMethod = buildClass.getMethod(function,null);
    values = (String)invokeMethod.invoke(mbuildClass, null);
    Log.i(TAG,"getProp values = "+values);
    return values;
}


public static void setBuildMethod(String function,String value1,String value2)throws Exception{
int ret = -1;
Class SystemProperties = Class.forName("android.os.Build");
Constructor constructor = SystemProperties.getConstructor();
Object buildMethod = constructor.newInstance();
    Method method = SystemProperties.getMethod(function,new Class[] { String.class, String.class });
    method.invoke(buildMethod,new Object[]{value1,value2});

}


public static void setBuildMethod_extern(String function,String value1)throws Exception{
int ret = -1;
Class SystemProperties = Class.forName("android.os.Build");
Constructor constructor = SystemProperties.getConstructor();
Object buildMethod = constructor.newInstance();
    Method method = SystemProperties.getMethod(function,new Class[] { String.class});
    method.invoke(buildMethod,new Object[]{value1});
}


}

使用方法 :

Hitv_IC.setBuildMethod_extern("setxxxx","123456789");//setxxxx为Build.java中的函数 ,123456789是参数

Hitv_IC.getBuildMethod("setxxxx");


0 0
原创粉丝点击