2014-11-17 49 views
-1
// Are we charging/charged? 

int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); 

//Determine the Current Battery Level 

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); 

裁判:http://developer.android.com/training/monitoring-device-state/battery-monitoring.html这段代码中-1的意义或用法是什么?

+2

如果找不到状态,它可能是应该返回的默认值 – John

+1

@John:的确是缺省值。 [见'getIntExtra' here](http://developer.android.com/reference/android/content/Intent.html) – Amadan

+0

回答你的问题.. – John

回答

0

如果你看开发商documentation for this method,它会告诉你:

公众诠释getIntExtra(字符串名称,诠释默认值)

检索扩展数据从意图。

参数:

  • 所需项目的名称。
  • defaultValue如果没有给定名称存储所需类型的值,则返回该值。

返回:

如果没有发现先前putExtra加入()项或者 默认值的值。