android.os.SystemProperties 在哪里?

发布于 2024-08-28 21:56:47 字数 573 浏览 12 评论 0原文

我正在查看 Android 相机代码,当我尝试导入 android.os.SystemProperties 时,找不到它。

这是我正在查看的文件:
https:// /android.googlesource.com/platform/packages/apps/Camera/+/eclair-release/src/com/android/camera/VideoCamera.java

我创建了一个新的 2.1 项目并尝试再次导入此命名空间,但是仍然找不到。我检查了 https://developer.android.com 并且未列出 SystemProperties

我错过了什么吗?

I'm looking at the Android Camera code, and when I try importing android.os.SystemProperties, it cannot be found.

Here is the file I'm looking at:
https://android.googlesource.com/platform/packages/apps/Camera/+/eclair-release/src/com/android/camera/VideoCamera.java

I created a new 2.1 project and tried importing this namespace again, but It still cannot be found. I checked https://developer.android.com and SystemProperties was not listed.

Did I miss something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一指流沙 2024-09-04 21:56:47

如果您使用“反射”选项,您可以使用下面的类

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
        }

    }
}

If you use the "reflection" option, you may use the class below

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
        }

    }
}
烟雨凡馨 2024-09-04 21:56:47

这是Android源代码中的类:

https://android.googlesource.com/platform/frameworks/base/+/eclair-release/core/java/android/os/SystemProperties.java

请参阅{@hide} 在 JavaDoc 类中?这意味着此类不会作为公共 SDK 的一部分导出。

相机应用程序使用它,因为它是内部的,并且他们不会使用公共 SDK 来构建它。

您仍然可以

  1. 通过反射或

  2. 通过获取源代码、删除@hide并创建自己的类来 获取此类
    定制 SDK。

然而,根据定义,您现在将“off SDK”,因此您的应用程序很可能会被破坏或在操作系统版本上获得不同的行为,因为 Android 人员会不遗余力地在版本之间更改此类。

This is the class in the Android source code:

https://android.googlesource.com/platform/frameworks/base/+/eclair-release/core/java/android/os/SystemProperties.java

See {@hide} in the class JavaDoc? That means this class won't be exported as part of the public SDK.

The camera app uses it as it's internal and they won't use the public SDK to build it.

You may still be able to get at this class

  1. by reflection or

  2. by getting the source, removing @hide and making your own
    customized SDK.

However pretty much by definition you are now going 'off SDK' and therefore your app may well be broken or get different behavior on OS versions as the Android folks will make little effort not to change this class between versions.

春风十里 2024-09-04 21:56:47

作为用户 Void 的答案发布的课程有很多不必要的东西。这是我的类,它使用 上的反射android.os.SystemProperties

/*
 * 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");
  }

}

The class posted as an answer from user Void has a bunch of unnecessary things. Here is my class that uses reflection on android.os.SystemProperties:

/*
 * 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");
  }

}
凡尘雨 2024-09-04 21:56:47

您可以执行 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();

You could exec out to the getprop command:

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();
笔落惊风雨 2024-09-04 21:56:47

经过一番折腾后,我终于得到了上面的反射代码,可以设置和创建新的本机系统属性,但有一些注意事项:

  1. 您需要成为系统用户,添加: android:sharedUserId="android.

  2. 您需要使用平台密钥对 APK 进行签名,我作弊并只是覆盖了 Eclipse 中的默认调试签名密钥,如下所示:
    http://stoned-android.blogspot.co.uk/2012_01_01_archive.html

  3. 本机系统属性服务有一个ACL,它控制对可以破坏关键空间的属性的所有写访问(例如sys.或debug.)。看
    /system/core/init/property_service.c:

    { "net.", AID_SYSTEM, 0 },
    {“开发”,AID_SYSTEM,0},
    {“运行时。”,AID_SYSTEM,0},
    {“硬件”,AID_SYSTEM,0},
    {“系统”,AID_SYSTEM,0},
    {“服务。”,AID_SYSTEM,0},
    {“无线局域网。”,AID_SYSTEM,0},
    { "dhcp.", AID_SYSTEM, 0 },

或者,如果您正在滚动自己的构建,则可以添加自己的密钥(如果您确实想要),但重用上述其中之一似乎更容易。

After a lot of messing around I've finally got reflection code above working to both set and create new native system properties, there are some caveats:

  1. You need to be the system user, add: android:sharedUserId="android.uid.system" to the manifest.

  2. You need to sign your APK with the platform key, I cheated and just overrode the default debug signing key in eclipse as show here:
    http://stoned-android.blogspot.co.uk/2012_01_01_archive.html

  3. The native system properties service has an ACL that controls all write access to properties you can subvert a key space (such as sys. or debug.). See
    /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 },

Or if you are rolling your own build you could add your own key if you really wanted but it seems easier to reuse one of the above.

笔芯 2024-09-04 21:56:47

以下是读取系统属性的方法。我也包含了一个后备方案:

import android.annotation.SuppressLint
import java.io.*
import java.lang.reflect.Method

object SystemProperties {
    private var failedUsingReflection = false
    private var getPropMethod: Method? = null

    @SuppressLint("PrivateApi")
    fun getProp(propName: String, defaultResult: String = ""): String {
        if (!failedUsingReflection) try {
            if (getPropMethod == null) {
                val clazz = Class.forName("android.os.SystemProperties")
                getPropMethod = clazz.getMethod("get", String::class.java, String::class.java)
            }
            return getPropMethod!!.invoke(null, propName, defaultResult) as String? ?: defaultResult
        } catch (e: Exception) {
            getPropMethod = null
            failedUsingReflection = true
        }
        var process: Process? = null
        try {
            process = Runtime.getRuntime().exec("getprop \"$propName\" \"$defaultResult\"")
            val reader = BufferedReader(InputStreamReader(process.inputStream))
            return reader.readLine()
        } catch (e: IOException) {
        } finally {
            process?.destroy()
        }
        return defaultResult
    }
}

使用反射它的工作速度比从进程输出中读取要快得多。在我测试的某些设备(小米红米 8)上大约快了 30 倍。

所以值得尝试一下。

Here's how you can read from the system properties. I've included a fallback too :

import android.annotation.SuppressLint
import java.io.*
import java.lang.reflect.Method

object SystemProperties {
    private var failedUsingReflection = false
    private var getPropMethod: Method? = null

    @SuppressLint("PrivateApi")
    fun getProp(propName: String, defaultResult: String = ""): String {
        if (!failedUsingReflection) try {
            if (getPropMethod == null) {
                val clazz = Class.forName("android.os.SystemProperties")
                getPropMethod = clazz.getMethod("get", String::class.java, String::class.java)
            }
            return getPropMethod!!.invoke(null, propName, defaultResult) as String? ?: defaultResult
        } catch (e: Exception) {
            getPropMethod = null
            failedUsingReflection = true
        }
        var process: Process? = null
        try {
            process = Runtime.getRuntime().exec("getprop \"$propName\" \"$defaultResult\"")
            val reader = BufferedReader(InputStreamReader(process.inputStream))
            return reader.readLine()
        } catch (e: IOException) {
        } finally {
            process?.destroy()
        }
        return defaultResult
    }
}

Using reflection it works much faster than reading from the process output. About x30 faster on some device I tested (Xiaomi Redmi 8).

So it's worth to try it instead.

一口甜 2024-09-04 21:56:47

经过大量搜索,我找到了一种为 Android 设置系统属性的方法。
我无法找到 Android Lollipop 版本的解决方案。但我成功地做到了这一点。为了设置系统属性,我们需要使用:

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

After lots of searching I found a way to set the system property for Android.
I was not able to find solution for Android Lollipop version. But I was successful in doing so. For setting system property we need to use :

import android.os.SystemProperties
SystemProperties.set(key, value).

e.g. SystemProperties.set("sys.android", 5.0)

Now you need to give permissions to the new system property
Go to /home/inkkashy04/Android_Lollypop/external/sepolicy/property_contexts
and give suitable permission to your property

sys.android u:object_r:system_prop:s0

Now after flashing your image you can see your system properties listed by command:

adb shell getprop
恬淡成诗 2024-09-04 21:56:47

梯度方法:

String SDK_DIR = System.getenv("ANDROID_SDK_HOME")
if(SDK_DIR == null) {
    Properties props = new Properties()
    props.load(new FileInputStream(project.rootProject.file("local.properties")))
    SDK_DIR = props.get('sdk.dir');
}
dependencies {
    compileOnly files("${SDK_DIR}/platforms/android-25/data/layoutlib.jar")
}

Gradle approach :

String SDK_DIR = System.getenv("ANDROID_SDK_HOME")
if(SDK_DIR == null) {
    Properties props = new Properties()
    props.load(new FileInputStream(project.rootProject.file("local.properties")))
    SDK_DIR = props.get('sdk.dir');
}
dependencies {
    compileOnly files("${SDK_DIR}/platforms/android-25/data/layoutlib.jar")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文