2016-08-19 23 views
0

我想为我的应用程序使用帮助器方法isLowRamDevice,该应用程序可以流式处理视频。当我支持API级别15的设备时,我不得不使用ActivityManagerCompat.isLowRamDevice()。 我真的很困惑,即使我使用的是旧设备,它总是返回错误。然后我检查方法本身并看到这个:ActivityManagerCompat.isLowRamDevice无用,总是返回false

public static boolean isLowRamDevice(@NonNull ActivityManager am) { 
     if (Build.VERSION.SDK_INT >= 19) { 
      return ActivityManagerCompatKitKat.isLowRamDevice(am); 
     } 
     return false; 
    } 

所以难怪它总是在我的Android 4.0.4设备上返回false。但对我来说这绝对没有意义。或者我错过了什么?

回答

2

所以难怪它始终返回false

它并不总是返回false

在运行Android 4.3或更早版本的设备上,它将始终返回false。这是因为当时不存在用作低RAM设备的系统标志。

在运行Android 4.4或更高版本的设备,它会返回系统标志的值对于这是否是一个低-RAM设备或不:

/** 
* Returns true if this is a low-RAM device. Exactly whether a device is low-RAM 
* is ultimately up to the device configuration, but currently it generally means 
* something in the class of a 512MB device with about a 800x480 or less screen. 
* This is mostly intended to be used by apps to determine whether they should turn 
* off certain features that require more RAM. 
*/ 
public boolean isLowRamDevice() { 
    return isLowRamDeviceStatic(); 
} 

/** @hide */ 
public static boolean isLowRamDeviceStatic() { 
    return "true".equals(SystemProperties.get("ro.config.low_ram", "false")); 
} 

(从the ActivityManager source code

AFAIK,低RAM设备大多将是Android One设备。根据您获得设备的位置,您可能不会遇到这些设备之一。

+0

当然,但运行Android 4.3或更高版本的设备更可能是lowRamDevices。那么为什么他们通过支持lib使这种方法适用于旧设备?这绝对没有意义。 – JensJensen

+0

@JensJensen:“运行Android 4.3或更早版本的设备更可能是lowRamDevices” - 不一定是Google对低RAM设备的定义。 “那么为什么他们通过支持lib使这种方法适用于较老的设备呢?” - 几乎所有以'... Compat'结尾的类都以这种方式工作。他们呼吁在兼容设备上实现真正的实现,并在旧设备上返回一些存根。有时候,存根更复杂。在这种情况下,我同意他们应该根据实际的设备RAM获得一个值。 – CommonsWare

+0

该实现与其他支持库一致。这听起来像你想把旧设备视为低RAM,所以我会说你最好将抽象调用和检查API版本和isLowRamDevice()标志放在一起。 –