Determining and Monitoring the Docking State and Type

  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 Intent, 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_DOCK_EVENT action. Because it's sticky, you don't need to register a BroadcastReceiver. You can simply callregisterReceiver() 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, -);
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 device is docked or undocked, the ACTION_DOCK_EVENT 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:

<action android:name="android.intent.action.ACTION_DOCK_EVENT"/>

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

Android 性能优化(13)网络优化( 9)Determining and Monitoring the Docking State and Type的更多相关文章

  1. 《Android开发艺术探索》读书笔记 (13) 第13章 综合技术、第14章 JNI和NDK编程、第15章 Android性能优化

    第13章 综合技术 13.1 使用CrashHandler来获取应用的Crash信息 (1)应用发生Crash在所难免,但是如何采集crash信息以供后续开发处理这类问题呢?利用Thread类的set ...

  2. Android性能优化典范(二)

    Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...

  3. android app性能优化大汇总(google官方Android性能优化典范 - 第2季)

    Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...

  4. Android性能优化典范 - 第2季

    Google发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的缩放,缓 ...

  5. Android 性能优化探究

    使用ViewStub动态载入布局.避免一些不常常的视图长期握住引用: ViewStub的一些特点: 1. ViewStub仅仅能Inflate一次,之后ViewStub对象被置空:某个被ViewStu ...

  6. android 性能优化

    本章介绍android高级开发中,对于性能方面的处理.主要包括电量,视图,内存三个性能方面的知识点. 1.视图性能 (1)Overdraw简介 Overdraw就是过度绘制,是指在一帧的时间内(16. ...

  7. Android性能优化典范第二季

      Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitma ...

  8. Android性能优化典范第一季

    2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...

  9. [转]Android性能优化典范

    2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...

随机推荐

  1. systemtap 作用-- SystemTap使用技巧

    http://blog.csdn.net/wangzuxi/article/details/42849053

  2. 【SQL Server 学习系列】-- SQL查询数据库表字段值不为空或Null的所有列

    ) set @TableName = 'Agency' -- 表名 declare @querySql nvarchar(max) set @querySql = 'select ' ) declar ...

  3. 将mysql数据库数据导出为.sql文件

    打开Navicat ,在我们要到处的数据上面右击鼠标,然后弹出的快捷菜单上点击“转储SQL 文件”,在再次弹出的子菜单项中选择第一个“数据跟结构”.

  4. SLF4J 和 Logback 在 Maven 项目中的使用方法

    原文:http://blog.csdn.net/llmmll08/article/details/70217120 本文介绍 SLF4J 和 Logback 在 Maven 项目中的用法,包括日志框架 ...

  5. 安卓数据传递之---putextra与putextras

    一.public Intent putExtra (String name, double[] value) 设置方法 intent.putExtra("aaa", "b ...

  6. psping

    psping工具功能主要包括:ICMP Ping.TCP Ping.延迟测试.带宽测试,是微软出品. 下载地址:https://download.sysinternals.com/files/PSTo ...

  7. Fragment进阶(五)-----&gt;监听fragment回退事件

    activity_main.xml <? xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  8. Javascript基本概念梳理

    javascript里的数据类型: 原始类型:数字,字符串.布尔值.(原始值:null,undefined) 对象类型:键值对,数组,function,全局对象(MATH,JSON) 保留字: abs ...

  9. CGlib小记

    CGlib是一个强大的代码生成包.常被用于各种AOP框架,提供"拦截"功能. JDK本身就为控制要訪问的对象提供了一 种途径,动态代理Proxy. 可是被代理的累必须实现一个或多个 ...

  10. Eclipse Android环境配置

    1.离线安装ADT插件,先将ZIP包下载 Help- Install New Software- Add 重启 2.WIndows -Preference设置SDK目录