Android編程實(shí)現(xiàn)簡單流量管理功能實(shí)例
本文實(shí)例講述了Android編程實(shí)現(xiàn)簡單流量管理功能的方法。分享給大家供大家參考,具體如下:
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();
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;
}
}
traffic_manager.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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>
traffic_manager_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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>
traffic_title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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="圖標(biāo)" />
<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>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android通信方式總結(jié)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- android編程實(shí)現(xiàn)設(shè)置、打開wifi熱點(diǎn)共享供他人連接的方法
- Android獲取當(dāng)前已連接的wifi信號強(qiáng)度的方法
- 在Android里完美實(shí)現(xiàn)基站和WIFI定位
- android開發(fā)教程之wifi開發(fā)示例
- Android仿水波紋流量球進(jìn)度條控制器
- Android編程實(shí)現(xiàn)監(jiān)控各個(gè)程序流量的方法
- 解析android 流量監(jiān)測的實(shí)現(xiàn)原理
- android wifi信號強(qiáng)度等級區(qū)分的修改介紹
- Android中判斷有無可用網(wǎng)絡(luò)的代碼(是否是3G或者WIFI網(wǎng)絡(luò))
- Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽詳解
相關(guān)文章
Android 判斷日期是否在一年以內(nèi)的算法實(shí)例
下面小編就為大家?guī)硪黄狝ndroid 判斷日期是否在一年以內(nèi)的算法實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Android實(shí)現(xiàn)列表時(shí)間軸
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)列表時(shí)間軸效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
android實(shí)現(xiàn)滑動(dòng)解鎖
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)滑動(dòng)解鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
android非RxJava環(huán)境下使用Handler實(shí)現(xiàn)預(yù)加載
這篇文章主要介紹了android非RxJava環(huán)境下使用Handler實(shí)現(xiàn)預(yù)加載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android ListView的OnItemClickListener詳解
這篇文章主要介紹了Android ListView的OnItemClickListener詳解的相關(guān)資料,涉及到OnItemClickListener的position和id參數(shù)做詳細(xì)的解釋的知識點(diǎn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-07-07
Android 使用selector改變按鈕狀態(tài)實(shí)例詳解
這篇文章主要介紹了Android 使用selector改變按鈕狀態(tài)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android自定義View模仿即刻點(diǎn)贊數(shù)字切換效果實(shí)例
有一個(gè)項(xiàng)目是仿即刻的點(diǎn)贊,這篇文章主要給大家介紹了關(guān)于Android自定義View模仿即刻點(diǎn)贊數(shù)字切換效果的相關(guān)資料,文中通過示例代碼介紹 的非常詳細(xì),需要的朋友可以參考下2022-12-12
Android編程實(shí)現(xiàn)動(dòng)態(tài)支持多語言的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)動(dòng)態(tài)支持多語言的方法,涉及Android資源、控件及屬性相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

