編輯:關於 Android 程式設計
想在自己的應用裡做一個流量監控頁?Android 提供的 TrafficStats 類足以勝任。它可以讀取整體行動網路與 Wi-Fi 的累計流量,也能按 UID 查詢單個應用的上傳/下載位元組數。本文以一個完整範例說明實作方式。
package cn.itcast.mobilesafe.ui;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.TrafficStats;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.itcast.mobilesafe.R;
import cn.itcast.mobilesafe.util.TextForMater;
public class TrafficManagerActivity extends Activity {
private TextView _3gTotal;
private TextView wifiTotal;
private ListView content;
private String mobileTraffic;
private String wifiTraffic;
private PackageManager pm;
private TrafficAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pm = getPackageManager();
setContentView(R.layout.traffic_manager);
_3gTotal = (TextView) this.findViewById(R.id._3gTotal);
wifiTotal = (TextView) this.findViewById(R.id.wifiTotal);
content = (ListView) this.findViewById(R.id.content);
setTotalData();
adapter = new TrafficAdapter();
content.addHeaderView(View.inflate(this, R.layout.traffic_title, null));
content.setAdapter(adapter);
}
private void setTotalData() {
long mobileRx = TrafficStats.getMobileRxBytes();
long mobileTx = TrafficStats.getMobileTxBytes();
long totalRx = TrafficStats.getTotalRxBytes();
long totalTx = TrafficStats.getTotalTxBytes();
// Wi-Fi 流量 = 總流量 - 行動網路流量
long wifiRx = totalRx - mobileRx;
long wifiTx = totalTx - mobileTx;
mobileTraffic = TextForMater.getDataSize(mobileRx + mobileTx);
_3gTotal.setText(mobileTraffic);
wifiTraffic = TextForMater.getDataSize(wifiTx + wifiRx);
wifiTotal.setText(wifiTraffic);
}
private class TrafficAdapter extends BaseAdapter {
List<ResolveInfo> resolveInfos;
public TrafficAdapter() {
super();
Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
resolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
}
@Override
public int getCount() {
return resolveInfos.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (null == convertView) {
view = View.inflate(getApplicationContext(), R.layout.traffic_item, null);
} else {
view = convertView;
}
ViewHolder holder = new ViewHolder();
holder.iv_traffic_icon = (ImageView) view.findViewById(R.id.iv_traffic_icon);
holder.tv_traffic_name = (TextView) view.findViewById(R.id.tv_traffic_name);
holder.tv_traffic_tx = (TextView) view.findViewById(R.id.tv_traffic_tx);
holder.tv_traffic_rx = (TextView) view.findViewById(R.id.tv_traffic_rx);
ResolveInfo info = resolveInfos.get(position);
String appName = info.loadLabel(pm).toString();
holder.tv_traffic_name.setText(appName);
Drawable icon = info.loadIcon(pm);
holder.iv_traffic_icon.setImageDrawable(icon);
int uid = info.activityInfo.applicationInfo.uid;
holder.tv_traffic_rx.setText(
TextForMater.getDataSize(TrafficStats.getUidRxBytes(uid)));
holder.tv_traffic_tx.setText(
TextForMater.getDataSize(TrafficStats.getUidTxBytes(uid)));
return view;
}
}
static class ViewHolder {
ImageView iv_traffic_icon;
TextView tv_traffic_name;
TextView tv_traffic_tx;
TextView tv_traffic_rx;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2G/3G 總流量" />
<TextView
android:id="@+id/_3gTotal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Wifi 總流量" />
<TextView
android:id="@+id/wifiTotal"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</TableRow>
</TableLayout>
<SlidingDrawer
android:id="@+id/ll_sd_traffic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/content"
android:handle="@+id/handle"
android:orientation="vertical">
<ImageView
android:id="@id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/notification" />
<ListView
android:id="@id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</SlidingDrawer>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_traffic_icon"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv_traffic_name"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="名稱" />
<TextView
android:id="@+id/tv_traffic_tx"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="上傳" />
<TextView
android:id="@+id/tv_traffic_rx"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="下載" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="圖標" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="名稱" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="上傳" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="下載" />
</LinearLayout>
Wi-Fi 流量是算出來的。TrafficStats 只提供 getTotalRxBytes/getTotalTxBytes(全部網路)與 getMobileRxBytes/getMobileTxBytes(行動網路),兩者相減即為 Wi-Fi 用量。這也是為什麼程式碼中沒有直接呼叫某個「getWifi」方法。
按 UID 統計是關鍵。getUidRxBytes(uid) 與 getUidTxBytes(uid) 回傳的是該 UID 自裝置啟動以來的累計值。程式中透過 queryIntentActivities 取得所有有啟動入口的應用,再從 ResolveInfo 中取出 applicationInfo.uid 進行查詢。
注意統計口徑。TelemetryStats 的計數從開機起累計,並非「本月用量」。若要做月度報表,需要自己記錄歷史基線並做差值計算。
ListView 的 ViewHolder 優化。範例中 convertView 為空時才 inflate,並把四個子 View 存入 ViewHolder,避免每次都呼叫 findViewById,長列表捲動會明顯順暢。不過範例每次都重新 new 了一個 ViewHolder,實務上應改成「convertView 為空時才建立並 setTag」。
流量格式化。TextForMater.getDataSize() 負責把位元組數轉為 B/KB/MB 等易讀單位,建議自行實作時一併處理進位與小數位。
Android 中 View 實現彈性滑動的三種方法——開發藝術探索筆記
一、什麼是彈性滑動 彈性滑動又稱漸進式滑動,做法是把一次大位移拆成許多小位移,在一段時間內逐步完成,讓滑動看起來有加速減速的過渡感,而不是硬邦邦地瞬移。所有實作方式背後都
RxJava 過濾類操作符整理:debounce、filter、take、skip 十式
這篇把 RxJava 中常見的「過濾」家族操作符做個集中整理,每一項都附上最小可運行的範例與說明。 一、debounce 只有在一段指定的時間內沒有再發射新資料時,才把
從已 Root 的 Android 手機匯出 App 的 DB 檔
前提 若要從 Android 裝置匯出 App 的 SQLite 資料庫,通常需要裝置已 Root,並能透過 adb 取得權限。此操作只應在自己擁有的裝置或已獲授權的測試
vivo X9 有幾個版本?標準版與高配版差異比較
vivo X9 是 X7 的後繼機型,外觀變化不大,主要改動在背面天線:採用類似弧線設計,金屬背面的覆蓋比例更高,整體視覺更俐落。 最大的升級在前置鏡頭 X9 把前置