Best Practices for Performance_4.Optimizing Battery Life 获取充电状态、电池信息,"sticky"类型的广播
2014-03-27 15:26
489 查看
http://developer.android.com/training/monitoring-device-state/index.html
http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
就会返回这个广播,就像新广播的一样。
这时调用registerReceiver 传入的 receiver 可为null, 直接返回广播的Intent。如下面获取电池状态的方法。
若没有,返回null,有多个 "sticky"广播符合 IntentFilter 时,由系统决定返回哪一个。
只能动态代码注册接收的广播:
Intent.ACTION_BATTERY_CHANGED
可以静态注册注册接收的广播:
android.intent.action.ACTION_POWER_CONNECTED
android.intent.action.ACTION_POWER_DISCONNECTED
android.intent.action.ACTION_BATTERY_LOW
android.intent.action.ACTION_BATTERY_OKAY
PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。
SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯
SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度
ACQUIRE_CAUSES_WAKEUP:强制使屏幕亮起,这种锁主要针对一些必须通知用户的操作.
ON_AFTER_RELEASE:当锁被释放时,保持屏幕亮起一段时间
用静态广播的方法:
Receiver接受到的Intent同上所述。
.Car
.Desk
.Low-End (Analog) Desk
.High-End (Digital) Desk
Note that the latter two options were only introduced to Android in API level 11
权限:
<action android:name="android.intent.action.ACTION_DOCK_EVENT"/>
//监听网络变化,这个广播会触发得比较频繁,从net->wifi 或者相反都会触发。
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
0. stickey 广播
系统会发一些 "sticky" 的广播,send出来后会一直保留着,当调用 registerReceiver 方法的 IntentFilter 符合其要求时,就会返回这个广播,就像新广播的一样。
这时调用registerReceiver 传入的 receiver 可为null, 直接返回广播的Intent。如下面获取电池状态的方法。
若没有,返回null,有多个 "sticky"广播符合 IntentFilter 时,由系统决定返回哪一个。
只能动态代码注册接收的广播:
Intent.ACTION_BATTERY_CHANGED
可以静态注册注册接收的广播:
android.intent.action.ACTION_POWER_CONNECTED
android.intent.action.ACTION_POWER_DISCONNECTED
android.intent.action.ACTION_BATTERY_LOW
android.intent.action.ACTION_BATTERY_OKAY
1. 监听充电状态,电池状态
一般在AC充电器充电的时候,可以加快后台的更新速率,USB充电的时候要减小,用电池的时候要更低。IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = mContext.registerReceiver(null, ifilter); // Are we charging / charged? 是否正在充电,是否充满 int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; // How are we charging? 用什么线充电,USB还是AC线 int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; //可返回这些信息,有电量,电池类型等等: // [icon-small=17302814, scale=100, present=true, technology=Li-poly, level=97, // voltage=4206, status=2, invalid_charger=0, plugged=2, health=2, temperature=310] //当前剩余百分之多少的电量: int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = level / (float)scale;
PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。
SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯
SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度
ACQUIRE_CAUSES_WAKEUP:强制使屏幕亮起,这种锁主要针对一些必须通知用户的操作.
ON_AFTER_RELEASE:当锁被释放时,保持屏幕亮起一段时间
用静态广播的方法:
Receiver接受到的Intent同上所述。
也可以用静态注册的广播来监听充电状态: <receiver android:name=".PowerConnectionReceiver"> <intent-filter> <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/> </intent-filter> </receiver> 监听电量变低或者充满的广播: <receiver android:name=".BatteryLevelReceiver"> <intent-filter> <action android:name="android.intent.action.ACTION_BATTERY_LOW"/> <action android:name="android.intent.action.ACTION_BATTERY_OKAY"/> </intent-filter> </receiver>
2. Determining and Monitoring the Docking State and Type
//Intent.ACTION_DOCK_EVENT也是个 "sticky" 的广播。 IntentFilter ifilter = new IntentFilter(Intent.ACTION_DOCK_EVENT); Intent dockStatus = context.registerReceiver(null, ifilter); int dockState = dockStatus.getIntExtra(Intent.EXTRA_DOCK_STATE, -1); boolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED; boolean isCar = dockState == Intent.EXTRA_DOCK_STATE_CAR; boolean isDesk = dockState == Intent.EXTRA_DOCK_STATE_DESK || dockState == Intent.EXTRA_DOCK_STATE_LE_DESK || dockState == Intent.EXTRA_DOCK_STATE_HE_DESK;
.Car
.Desk
.Low-End (Analog) Desk
.High-End (Digital) Desk
Note that the latter two options were only introduced to Android in API level 11
权限:
<action android:name="android.intent.action.ACTION_DOCK_EVENT"/>
3. 监听和获取网络连接的状态。
如果网络不通就没必要在后台去访问网络。ConnectivityManager cm =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
//监听网络变化,这个广播会触发得比较频繁,从net->wifi 或者相反都会触发。
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
4. 动态的开启或者关闭 广播接收器
可以根据需要来设置receiver 是否要工作,这种方式设置后,即使重启手机了也是有效的。ComponentName receiver = new ComponentName(context, myReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
/方法原型 public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags); newState The new enabled state for the component. The legal values for this state are: COMPONENT_ENABLED_STATE_ENABLED, COMPONENT_ENABLED_STATE_DISABLED, COMPONENT_ENABLED_STATE_DEFAULT The last one removes the setting, thereby restoring the component's state to whatever was set in it's manifest (or enabled, by default). flags Optional behavior flags: DONT_KILL_APP or 0. DONT_KILL_APP to indicate that you don't want to kill the app containing the component. Be careful when you set this since changing component states can make the containing application's behavior unpredictable.
相关文章推荐
- 通过广播方式获取当前网络类型,如果是Wifi,获取对应的信息(附加有root之后可实现保存的无线网名称及密码获取)
- Android获取电池充电状态的方式
- js 获取input type="file" 选择的文件大小、文件名称、上次修改时间、类型等信息
- Android 获取 Wifi 信号强度,检测是 Wifi 或者流量,获取手机电池电量及充电状态
- Client API - Best Practices for performance tuning
- "此页的状态信息无效,可能已损坏"的原因和解决方法
- Best Practices for Increasing Website Performance (1)
- 【Android Training - Performance】优化电池续航能力[Lesson 1 - 监测设备的电量与充电状态]
- 附加类型“UniversalReviewSystem.Models.ApplicationUser”的实体失败,因为相同类型的其他实体已具有相同的主键值。在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值
- Best Practices for Performance_1、2 memory、Tips 性能和小的优化点、 onTrimMemory
- Best Practices for Increasing Website Performance (2)
- Best Practices for Performance PPT
- Performance best practices for Web services
- Coding Standards and Best Practices for "appobjects" Files
- 用JS获取不到元素样式信息的"Bug"?
- Best practices for .Net Performance - I
- Best Practices for Performance_3.Improving Layout Performance 优化布局
- Android获取电池充电状态的方式
- 错误:因为相同类型的其他实体已具有相同的主键值。在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 解决方法
- s 获取input type="file" 选择的文件大小、文件名称、上次修改时间、类型等信息