博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Determining and Monitoring the Docking State and Type 判断并监测设备的停驻状态与类型
阅读量:4046 次
发布时间:2019-05-24

本文共 2360 字,大约阅读时间需要 7 分钟。

Android devices can be docked into several different kinds of docks. These include car or home docks and digital versus analog docks. The dock-state is typically closely linked to the charging state as many docks provide power to docked devices.

How the dock-state of the phone affects your update rate depends on your app. You may choose to increase the update frequency of a sports center app when it's in the desktop dock, or disable your updates completely if the device is car docked. Conversely, you may choose to maximize your updates while car docked if your background service is updating traffic conditions.

The dock state is also broadcast as a sticky , allowing you to query if the device is docked or not, and if so, in which kind of dock.

Determine the Current Docking State

The dock-state details are included as an extra in a sticky broadcast of the action. Because it's sticky, you don't need to register a . You can simply call passing in null as the broadcast receiver as shown in the next snippet.

IntentFilter ifilter = new IntentFilter(Intent.ACTION_DOCK_EVENT);Intent dockStatus = context.registerReceiver(null, ifilter);

You can extract the current docking status from the EXTRA_DOCK_STATE extra:

int dockState = battery.getIntExtra(EXTRA_DOCK_STATE, -1);boolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;

Determine the Current Dock Type

If a device is docked, it can be docked in any one of four different type of dock:

  • 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, so it's good practice to check for all three where you are only interested in the type of dock rather than it being digital or analog specifically:

boolean isCar = dockState == EXTRA_DOCK_STATE_CAR;boolean isDesk = dockState == EXTRA_DOCK_STATE_DESK ||                  dockState == EXTRA_DOCK_STATE_LE_DESK ||                 dockState == EXTRA_DOCK_STATE_HE_DESK;

Monitor for Changes in the Dock State or Type

Whenever the the device is docked or undocked, the action is broadcast. To monitor changes in the device's dock-state, simply register a broadcast receiver in your application manifest as shown in the snippet below:

You can extract the dock type and state within the receiver implementation using the same techniques described in the previous step.

转载地址:http://iagdi.baihongyu.com/

你可能感兴趣的文章
GIT学习(三)_远程仓库(GITHUB)
查看>>
Linux常用命令
查看>>
常用shell脚本(转)
查看>>
linux基础篇读书笔记
查看>>
linux基础篇读书笔记2_后台执行命令
查看>>
Linux常用命令详解(一)
查看>>
Linux常用命令详解(二)_find命令
查看>>
Linux常用命令详解(三)_权限管理
查看>>
Linux常用命令详解(四)_文件过滤分割、统计、kill
查看>>
Linux常用命令详解(五)_性能检测
查看>>
初入操作系统
查看>>
初入Oracle 编程艺术_基本概念
查看>>
Linux常用命令详解(六)_网络
查看>>
Linux常用命令详解(七)_文件压缩、解压
查看>>
Log4j
查看>>
Oracle触发器用法实例详解
查看>>
MySQL学习(入门)
查看>>
单例设计模式
查看>>
工厂设计模式
查看>>
代理设计模式
查看>>