如何导入 android.os.SystemProperties

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10409664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:36:12  来源:igfitidea点击:

how to import android.os.SystemProperties

android

提问by Sunny Kumar Aditya

I am trying to use

我正在尝试使用

SystemProperties.get();

but not able to import the android.os.SystemPropertiespackage bit of googling tells me its hidden, is there any way around this ?

但无法导入android.os.SystemProperties包,谷歌搜索告诉我它是隐藏的,有什么办法可以解决这个问题吗?

回答by Joseph Johnson

SystemPropertiesis a hidden class that is only available to platform-level code. You could try to access it via reflection, but this is not recommended.

SystemProperties是一个隐藏类,仅对平台级代码可用。您可以尝试通过反射访问它,但不建议这样做。

It is easy to confuse Android's SystemPropertiesclass with the old-style Java way of doing properties, namely java.lang.System.getProperty(). Note that these are two totally separate systems. An important difference is that the Java mechanism works only within a given process, whereas the Android SystemPropertiesworks across processes.

很容易将 Android 的SystemProperties类与旧式 Java 处理属性的方式(即java.lang.System.getProperty(). 请注意,这是两个完全独立的系统。一个重要的区别是 Java 机制只在给定的进程内工作,而 AndroidSystemProperties是跨进程工作的。

As an app developer, although you don't normally get a Java-based API into Android's Android SystemProperties, you can inspect the SystemPropertiesat the command-line using

作为应用程序开发人员,虽然您通常不会在 Android 的 Android 中获取基于 Java 的 API SystemProperties,但您可以SystemProperties在命令行中使用

adb shell getprop

回答by Anil Jadhav

You can import import android.os.Bundle;and then you can access System properties using System.getProperty("os.name");

你可以导入import android.os.Bundle; 然后您可以使用System.getProperty("os.name");访问系统属性

you can access any property by using System.getProperty('Property name');

您可以使用 System.getProperty('Property name'); 访问任何属性;

回答by moasm

add 'layoutlib.jar" to your project's Java Build Path, and then, you can use "import android.os.SystemProperties;"

将“layoutlib.jar”添加到您项目的Java Build Path,然后,您可以使用“import android.os.SystemProperties;”

回答by and0421

if want to get device property, for OS information you may check http://developer.android.com/reference/android/os/Build.html

如果想获取设备属性,对于操作系统信息,您可以查看http://developer.android.com/reference/android/os/Build.html

just need to import android.os.Build;

只需要导入android.os.Build;

回答by K.H.

One hacky way is to create your own SystemPropertiesclass on your classpath so that your code compiles and works and then exclude it from your build. If class then exists on android device, it works fine. I do this quite often for very specific situations, but I wouldn't recommend this on commercial application for various versions and devices.

一种hacky 方法是在您的SystemProperties类路径上创建您自己的类,以便您的代码编译并运行,然后将其从您的构建中排除。如果类然后存在于 android 设备上,它工作正常。我经常在非常特定的情况下这样做,但我不建议在各种版本和设备的商业应用程序中使用它。

You can base it on source codeclass and just leave api that you need.

你可以基于源代码类,只留下你需要的 api。

Example class:

示例类:

package android.os;

public class SystemProperties {

    public static String get(String key) {
        return "";
    }

}

Then you just need exclude that class from your android/sourcesets/main in gradle file, for example:

然后你只需要从你的 android/sourcesets/main 中的 gradle 文件中排除那个类,例如:

sourceSets {
    main {
        java {
            exclude 'android/**/*.java'
        }
    }
}

回答by MMJ

One thing you can try is, comment the

您可以尝试的一件事是,评论

LOCAL_SDK_VERSION := current

LOCAL_SDK_VERSION := 当前

in Android.mk. Using this way you can able to call hidden apis from the app

在 Android.mk 中。使用这种方式,您可以从应用程序调用隐藏的 api