android.os.SystemProperties在哪里?
我在看Android的代码,当我尝试导入android.os.SystemProperties
,它不能被发现。
这里是我正在看的文件:
https://android.googlesource.com/platform/packages/apps/Camera/+/eclair-release/src/com/android/camera/VideoCamera.java
我创build了一个新的2.1项目,并尝试再次导入这个命名空间,但它仍然无法find。 我检查了https://developer.android.com和SystemProperties
没有列出。
我错过了什么?
这是Android源代码中的类:
请参阅JavaDoc类中的{@hide}
这意味着这个类不会作为公共SDK的一部分被导出。
相机应用程序使用它,因为它是内部的,他们不会使用公共SDK来构build它。
你可能还能够上这门课
-
通过reflection或
-
通过获取源代码,删除
@hide
并制作自己的定制SDK。
然而,从定义上来说,你现在正在off SDK
,所以你的应用程序很可能会在操作系统版本上被破坏或得到不同的行为,因为Android人员将不费吹灰之力就不能在版本之间改变这个类。
如果您使用“reflection”选项,则可以使用下面的类
package com.etc.etc; 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 } } }
该类发布为用户Void的答案有一堆不必要的东西。 这里是我的类使用android.os.SystemProperties上的reflection:
/* * Copyright (C) 2015 Jared Rummler * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Gives access to the system properties store. The system properties store contains a list of * string key-value pairs. */ public class SystemProperties { private static final Class<?> SP = getSystemPropertiesClass(); /** * Get the value for the given key. */ public static String get(String key) { try { return (String) SP.getMethod("get", String.class).invoke(null, key); } catch (Exception e) { return null; } } /** * 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 */ public static String get(String key, String def) { try { return (String) SP.getMethod("get", String.class, String.class).invoke(null, key, def); } catch (Exception e) { return def; } } /** * 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 * sensitive). 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. */ public static boolean getBoolean(String key, boolean def) { try { return (Boolean) SP.getMethod("getBoolean", String.class, boolean.class) .invoke(null, key, def); } catch (Exception e) { return def; } } /** * 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 */ public static int getInt(String key, int def) { try { return (Integer) SP.getMethod("getInt", String.class, int.class).invoke(null, key, def); } catch (Exception e) { return def; } } /** * 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 */ public static long getLong(String key, long def) { try { return (Long) SP.getMethod("getLong", String.class, long.class).invoke(null, key, def); } catch (Exception e) { return def; } } private static Class<?> getSystemPropertiesClass() { try { return Class.forName("android.os.SystemProperties"); } catch (ClassNotFoundException shouldNotHappen) { return null; } } private SystemProperties() { throw new AssertionError("no instances"); } }
经过大量的搞乱我终于得到了上面的reflection代码,以设置和创build新的本机系统属性,有一些注意事项:
-
您需要成为系统用户,将:android:sharedUserId =“android.uid.system”添加到清单。
-
你需要使用平台密钥签署你的APK,我欺骗了,只是覆盖了Eclipse中默认的debugging签名密钥,如下所示: http : //stoned-android.blogspot.co.uk/2012_01_01_archive.html
-
本地系统属性服务具有一个ACL,用于控制对属性进行的所有写入访问,从而可以破坏密钥空间(如sys或debug)。 请参阅/system/core/init/property_service.c:
{“net。”,AID_SYSTEM,0},{“dev。”,AID_SYSTEM,0},{“runtime。”,AID_SYSTEM,0},{“hw。”,AID_SYSTEM,0},{“sys。”, AID_SYSTEM,0},{“service。”,AID_SYSTEM,0},{“wlan。”,AID_SYSTEM,0},{“dhcp。”,AID_SYSTEM,0},
或者,如果你正在编译自己的版本,你可以添加自己的密钥,如果你真的想要,但似乎更容易重用上述之一。
你可以执行getprop
命令:
String line = ""; try { Process ifc = Runtime.getRuntime().exec("getprop ro.hardware"); BufferedReader bis = new BufferedReader(new InputStreamReader(ifc.getInputStream())); line = bis.readLine(); } catch (java.io.IOException e) { } ifc.destroy();
大量的search后,我find了一种方法来设置Android的系统属性。 我无法findAndroid棒棒糖版本的解决scheme。 但是我这样做是成功的。 为了设置系统属性,我们需要使用:
import android.os.SystemProperties SystemProperties.set(key, value).
例如SystemProperties.set("sys.android", 5.0)
现在你需要赋予新的系统属性权限转到/home/inkkashy04/Android_Lollypop/external/sepolicy/property_contexts
并给予你的财产适当的权限
sys.android u:object_r:system_prop:s0
现在在刷新图像后,您可以看到您的系统属性按命令列出:
adb shell getprop
如果你想获得设备属性,你可以检查操作系统的信息
http://developer.android.com/reference/android/os/Build.html
- 使用Google Map API V2查找地图上两点之间的距离
- 活动<应用程序名称>泄露了最初在此处绑定的ServiceConnection <ServiceConnection名称> @ 438030a8
- Gradle:执行失败的任务':processDebugManifest'
- Android的webview速度慢
- 此Android SDK需要Android Developer Toolkit版本23.0.0或更高版本
- 如何更改新的TabLayout指标的颜色和高度
- 有LLVM IR库如何从Ubuntu交叉编译到iOS,Android,Windows和Mac?
- 如何在devise支持库的TabLayout中的Tabs之间设置分隔符?
- Android从选定的单选button获取价值