編輯:關於Android編程
本文實例講述了Android編程實現簡單流量管理功能的方法。分享給大家供大家參考,具體如下:
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="圖標" /> <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>
更多關於Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android通信方式總結》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
一、准備工作1、你需要android手機應用開發基礎2、科大訊飛語音識別SDK android版3、科大訊飛語音識別開發API文檔4、android手機關於科大訊飛SDK
android提供了四類常用的對話框,本文分享具體實現方法: 1.AlertDialog,功能最豐富,實際運用最廣泛 2.progressDialog,進度條對
一丶音樂播放頁實現功能1.音樂格信息顯示,大圖顯示2.播放功能,上一曲,下一曲,暫停3.音樂進度顯示4.切換播放模式二丶顯示效果三丶原理及代碼實現1.自定義接口回調的方法
一、概述最近和朋友聊天,發現一些灰色產業鏈通過批量反編譯市場上的apk,然後進行注入廣告,再重新打包上渠道。我想大家都不希望自己家的產品或者自己的app那麼容易被&ldq