Gdzie jest android.os.Systemy?

Patrzę na kod aparatu z Androidem i kiedy próbuję zaimportować android.os.SystemProperties, nie można go znaleźć.

Oto plik, którego szukam at:
https://android.googlesource.com/platform/packages/apps/Camera/+/eclair-release/src/com/android/camera/VideoCamera.java

Utworzyłem nowy projekt 2.1 i próbowałem ponownie zaimportować tę przestrzeń nazw, ale nadal nie można jej znaleźć. Sprawdziłam https://developer.android.com i SystemProperties nie zostały wymienione.

Czy Coś mnie ominęło?

Author: Vadim Kotov, 2010-04-14

8 answers

To jest klasa w kodzie źródłowym Androida:

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

Zobacz {@hide} w klasie JavaDoc? Oznacza to, że ta klasa nie będzie eksportowana jako część publicznego SDK.

Aplikacja aparatu używa go, ponieważ jest wewnętrzny i nie będą używać publicznego SDK, aby go zbudować.

Możesz nadal być w stanie dostać się na tę klasę

  1. Przez reflection lub

  2. Pozyskując źródło, usuwając @hide i tworząc własne dostosowane SDK.

Jednak dość dużo z definicji jesteś teraz będzie "off SDK " i dlatego Twoja aplikacja może być uszkodzony lub uzyskać różne zachowanie NA wersjach systemu operacyjnego, jak ludzie z Androidem będzie niewiele wysiłku, aby nie zmieniać tej klasy między wersjami.

 34
Author: Jim Blackler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-24 10:09:50

Jeśli używasz opcji "reflection", możesz użyć poniższej klasy

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
        }

    }
}
 63
Author: Void,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-27 18:23:46

Klasa zamieszczona jako odpowiedź od użytkownika Void ma masę niepotrzebnych rzeczy. Oto moja klasa, która wykorzystuje refleksję na android.os.Systemprzypisy:

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

}
 26
Author: Jared Rummler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-26 05:49:39

Po wielu bałaganach w końcu mam kod reflection powyżej pracujący zarówno nad ustawieniem, jak i utworzeniem nowych właściwości natywnych systemu, są pewne zastrzeżenia:

  1. Musisz być użytkownikiem systemu, dodaj: android: sharedUserId= " android.uid.system" do manifestu.

  2. Musisz podpisać APK kluczem platformy, oszukałem i po prostu przekroczyłem domyślny Klucz podpisywania debugowania w eclipse jako pokaz proszę.: http://stoned-android.blogspot.co.uk/2012_01_01_archive.html

  3. Natywny System properties Service posiada ACL, który kontroluje cały dostęp zapisu do właściwości, które można wywrócić do przestrzeni klucza (np. sys. lub debugować.). Zobacz też / 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 }, {"serwis.", AID_SYSTEM, 0 }, {"wlan.", AID_SYSTEM, 0 }, {"dhcp.", AID_SYSTEM, 0},

Lub jeśli kręcisz własną kompilację, możesz dodać swój własny klucz, jeśli naprawdę chcesz, ale wydaje się łatwiejsze ponowne użycie jednego z powyższych.

 15
Author: Cakey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-20 16:06:22

Możesz wykonać polecenie 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();
 15
Author: danS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-03 12:51:59

Po wielu poszukiwaniach znalazłem sposób, aby ustawić właściwość systemu dla Androida. Nie udało mi się znaleźć rozwiązania dla wersji Android Lollipop. Ale udało mi się to zrobić. Do Ustawienia Właściwości systemowej musimy użyć :

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

Np. SystemProperties.set("sys.android", 5.0)

Teraz musisz nadać uprawnienia do nowej właściwości systemu Przejdź do /home/inkkashy04/Android_Lollypop/external/sepolicy/property_contexts i dać odpowiednie pozwolenie na swoją własność

Sys.android u: object_r: system_prop: s0

Teraz po pokazaniu swojego obraz możesz zobaczyć swoje właściwości systemu wymienione przez polecenie:

adb shell getprop
 2
Author: khushank kashyap,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-24 10:11:58

Oto jak możesz odczytać właściwości systemu. Ja też dodałem fallback:

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
    }
}

Używanie reflection działa znacznie szybciej niż odczyt z wyjścia procesu. O x30 szybciej na jakimś urządzeniu, które testowałem (Xiaomi Redmi 8).

Warto więc spróbować.
 1
Author: android developer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-08-02 14:41:53

Podejście Gradle:

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")
}
 0
Author: Chulo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-03-28 09:34:51