gpt4 book ai didi

android - 如何为 android.os.systemproperties 设置反射?

转载 作者:行者123 更新时间:2023-11-29 02:13:39 32 4
gpt4 key购买 nike

所以我试图让这个应用程序从反射中调用 android.os.SystemProperties 但它有点痛苦,所以如果有人能提供帮助,我们将不胜感激。

所以这是让我失望的代码部分:

        String str1 = SystemPropertiesProxy.get("ro.url.changelog");
String str2 = SystemPropertiesProxy.get("ro.build.incremental");
String str4 = String.format("?version=%s", str2);
Object[] arrayOfObject1 = new Object[2];
arrayOfObject1[0] = str1;
arrayOfObject1[1] = str4;
String str5 = String.format("%s%s", arrayOfObject1);
...
myWebView.loadUrl(str5);

上面的代码给我错误:“String str1 = SystemPropertiesProxy.get(”ro.url.变更日志”)”这是我试图设置为访问 android.os.SystemProperties 的反射代码:

package com.package.name;

import java.io.File;
import java.lang.reflect.Method;
import android.content.Context;
import dalvik.system.DexFile;


public class SystemPropertiesProxy
{

/**
* This class cannot be instantiated
*/
private SystemPropertiesProxy(){

}

/**
* Get the value for the given key.
* @return an empty string if the key isn't found
* @throws IllegalArgumentException if the key exceeds 32 characters
*/
public static String get(Context context, String key) throws IllegalArgumentException {

String ret= "";

try{

ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");

//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[1];
paramTypes[0]= String.class;

Method get = SystemProperties.getMethod("get", paramTypes);

//Parameters
Object[] params= new Object[1];
params[0]= new String(key);

ret= (String) get.invoke(SystemProperties, params);

}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= "";
//TODO
}

return ret;

}

/**
* Get the value for the given key.
* @return if the key isn't found, return def if it isn't null, or an empty string otherwise
* @throws IllegalArgumentException if the key exceeds 32 characters
*/
public static String get(Context context, String key, String def) throws IllegalArgumentException {

String ret= def;

try{

ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");

//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= String.class;

Method get = SystemProperties.getMethod("get", paramTypes);

//Parameters
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new String(def);

ret= (String) get.invoke(SystemProperties, params);

}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= def;
//TODO
}

return ret;

}

/**
* Get the value for the given key, and return as an integer.
* @param key the key to lookup
* @param def a default value to return
* @return the key parsed as an integer, or def if the key isn't found or
* cannot be parsed
* @throws IllegalArgumentException if the key exceeds 32 characters
*/
public static Integer getInt(Context context, String key, int def) throws IllegalArgumentException {

Integer ret= def;

try{

ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");

//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= int.class;

Method getInt = SystemProperties.getMethod("getInt", paramTypes);

//Parameters
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new Integer(def);

ret= (Integer) getInt.invoke(SystemProperties, params);

}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= def;
//TODO
}

return ret;

}

/**
* Get the value for the given key, and return as a long.
* @param key the key to lookup
* @param def a default value to return
* @return the key parsed as a long, or def if the key isn't found or
* cannot be parsed
* @throws IllegalArgumentException if the key exceeds 32 characters
*/
public static Long getLong(Context context, String key, long def) throws IllegalArgumentException {

Long ret= def;

try{

ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties= cl.loadClass("android.os.SystemProperties");

//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= long.class;

Method getLong = SystemProperties.getMethod("getLong", paramTypes);

//Parameters
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new Long(def);

ret= (Long) getLong.invoke(SystemProperties, params);

}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= def;
//TODO
}

return ret;

}

/**
* Get the value for the given key, returned as a boolean.
* Values 'n', 'no', '0', 'false' or 'off' are considered false.
* Values 'y', 'yes', '1', 'true' or 'on' are considered true.
* (case insensitive).
* If the key does not exist, or has any other value, then the default
* result is returned.
* @param key the key to lookup
* @param def a default value to return
* @return the key parsed as a boolean, or def if the key isn't found or is
* not able to be parsed as a boolean.
* @throws IllegalArgumentException if the key exceeds 32 characters
*/
public static Boolean getBoolean(Context context, String key, boolean def) throws IllegalArgumentException {

Boolean ret= def;

try{

ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");

//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= boolean.class;

Method getBoolean = SystemProperties.getMethod("getBoolean", paramTypes);

//Parameters
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new Boolean(def);

ret= (Boolean) getBoolean.invoke(SystemProperties, params);

}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= def;
//TODO
}

return ret;

}

/**
* Set the value for the given key.
* @throws IllegalArgumentException if the key exceeds 32 characters
* @throws IllegalArgumentException if the value exceeds 92 characters
*/
public static void set(Context context, String key, String val) throws IllegalArgumentException {

try{

@SuppressWarnings("unused")
DexFile df = new DexFile(new File("/system/app/Settings.apk"));
@SuppressWarnings("unused")
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = Class.forName("android.os.SystemProperties");

//Parameters Types
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= String.class;

Method set = SystemProperties.getMethod("set", paramTypes);

//Parameters
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new String(val);

set.invoke(SystemProperties, params);

}
catch( IllegalArgumentException iAE ){
throw iAE;
}
catch( Exception e ){
//TODO
}

}
}

再次感谢任何帮助。谢谢。

最佳答案

你没写,这是编译错误

没有方法

public static String get(String key){}

在 SystemPropertiesProxy 中。使用

public static String get(Context context, String key):


SystemPropertiesProxy.get(context, "ro.url.changelog");

无论如何......使用私有(private)API不是一个好主意

关于android - 如何为 android.os.systemproperties 设置反射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5754697/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com