Modifying your Download Patterns Based on the Connectivity Type

本文讨论了如何通过Wi-Fi进行高效的数据传输以节省电池电量,并介绍了如何根据不同的无线网络类型调整预取缓存大小的方法。

Use Wi-Fi

In most cases a Wi-Fi radio will offer greater bandwidth at a significantly lower battery cost. As a result, you should endeavor to perform data transfers when connected over Wi-Fi whenever possible.

You can use a broadcast receiver to listen for connectivity changes that indicate when a Wi-Fi connection has been established to execute significant downloads, preempt scheduled updates, and potentially even temporarily increase the frequency of regular updates as described in Optimizing Battery Life lesson Determining and Monitoring the Connectivity Status.

Use Greater Bandwidth to Download More Data Less Often

When connected over a wireless radio, higher bandwidth generally comes at the price of higher battery cost. Meaning that LTE typically consumes more energy than 3G, which is in turn more expensive than 2G.

This means that while the underlying radio state machine varies based on the radio technology, generally speaking the relative battery impact of the state change tail-time is greater for higher bandwidth radios.

At the same time, the higher bandwidth means you can prefetch more aggressively, downloading more data over the same time. Perhaps less intuitively, because the tail-time battery cost is relatively higher, it's also more efficient to keep the radio active for longer periods during each transfer session to reduce the frequency of updates.

For example, if an LTE radio is has double the bandwidth and double the energy cost of 3G, you should download 4 times as much data during each session—or potentially as much as 10mb. When downloading this much data, it's important to consider the effect of your prefetching on the available local storage and flush your prefetch cache regularly.

You can use the connectivity manager to determine the active wireless radio, and modify your prefetching routines accordingly:

ConnectivityManager cm =
 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

TelephonyManager tm =
  (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
 
int PrefetchCacheSize = DEFAULT_PREFETCH_CACHE;
 
switch (activeNetwork.getType()) {
  case (ConnectivityManager.TYPE_WIFI): 
    PrefetchCacheSize = MAX_PREFETCH_CACHE; break;
  case (ConnectivityManager.TYPE_MOBILE): {
    switch (tm.getNetworkType()) {
      case (TelephonyManager.NETWORK_TYPE_LTE | 
            TelephonyManager.NETWORK_TYPE_HSPAP): 
        PrefetchCacheSize *= 4;
        break;
      case (TelephonyManager.NETWORK_TYPE_EDGE | 
            TelephonyManager.NETWORK_TYPE_GPRS): 
        PrefetchCacheSize /= 2;
        break;
      default: break;
    }
    break;
  }
  default: break;
}
### 优化布局性能并支持文本扩展的实现方法 在 Android 的 `LinearLayout` 布局中,为了提升性能并支持文本内容的动态扩展,可以通过禁用基准线对齐(`android:baselineAligned="false"`)以及合理配置 `LayoutParams` 来实现。 #### 禁用基准线对齐以提升性能 当 `LinearLayout` 中的子视图均为 `ViewGroup` 或不涉及文本基线对齐时,建议设置 `android:baselineAligned="false"`。这一操作可以避免系统进行不必要的基准线对齐计算,从而减少主线程的开销并提高渲染效率[^1]。此外,在某些场景下,如子视图的文本大小或样式差异较大时,基准线对齐可能无法达到预期效果,因此禁用该特性更为合理[^2]。 以下是一个示例代码片段,展示如何在 XML 文件中应用此设置: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:baselineAligned="false"> <TextView android:id="@+id/textView1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Short Text" /> <TextView android:id="@+id/textView2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="A much longer text that may expand dynamically based on content." /> </LinearLayout> ``` 通过将宽度设为 `0dp` 并配合 `layout_weight` 属性,可以让每个 `TextView` 根据其权重自动调整尺寸,同时确保文本能够正确扩展而不影响整体布局结构[^3]。 #### 支持文本扩展的 LayoutParams 配置 除了禁用基准线对齐外,还需要合理设置 `LayoutParams` 以支持文本内容的动态扩展。具体来说,对于水平方向上的 `LinearLayout`,可以将子视图的 `layout_width` 设为 `0dp` 并结合 `layout_weight` 属性分配空间比例;而对于垂直方向上的 `LinearLayout`,则应将 `layout_height` 设为 `0dp` 并使用 `layout_weight` 进行高度分配。这种方式不仅有助于文本内容根据实际需要扩展,还能保持良好的响应式布局特性。 此外,若希望文本能够在超出可用空间时自动换行,可通过设置 `android:singleLine="false"` 和 `android:maxLines` 来控制文本行为。例如: ```xml <TextView android:id="@+id/textView3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:singleLine="false" android:maxLines="3" android:text="This is a very long text which might need to wrap into multiple lines depending on the available space." /> ``` 上述配置允许文本在必要时换行显示,并限制最大行数以防止过度占用屏幕空间。 #### 综合优化策略 除了禁用基准线对齐和配置 `LayoutParams` 外,还可以采用其他优化手段进一步提升 `LinearLayout` 的性能表现: - **减少嵌套层级**:尽量简化布局层次结构,避免过多的嵌套 `ViewGroup`。 - **使用 ConstraintLayout 替代 LinearLayout**:对于复杂的 UI 场景,推荐使用 `ConstraintLayout`,它提供了更灵活的布局方式且通常具有更好的性能表现。 - **预加载资源**:提前加载可能使用的图片或其他资源,避免运行时频繁请求导致卡顿。 - **启用 GPU 渲染分析工具**:利用 Android 提供的开发者选项中的 GPU 渲染分析功能,识别潜在的绘制瓶颈。 通过以上方法,可以在保证良好用户体验的同时有效提升应用程序的整体性能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值