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程序设计有所帮助。

时间: 2024-08-18 08:23:21

Android编程实现简单流量管理功能实例的相关文章

Android编程实现简单流量管理功能实例_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编程实现换肤功能实例_Android

本文实例讲述了Android编程实现换肤功能的方法.分享给大家供大家参考,具体如下: 本系列专题培训适用范围:初级Android程序员,即有J2SE基础和Android初级水平.J2SE基础是指掌握JAVA语法,1.5.1.6新增的语法不完全掌握也没关系.了解基本的面向对象思想.能编写简单的J2SE程序,掌握基本的调试方法,熟悉Swing更好.Android初级是指掌握Activity.Service.BroadcastReceiver.Intent.SQLite.UI组件的使用,能参照例子编写

Android编程实现简单的UDP Client实例_Android

本文实例讲述了Android编程实现简单的UDP Client.分享给大家供大家参考,具体如下: 该代码在4.2.2内调试通过 1.记得加权限 <uses-permission android:name="android.permission.INTERNET"/> 注意:Android 4.0之后,就不能在主线程进行socket通信,否则会抛异常. 2.代码 MainActivity.java: package mao.example.quicksend; import

Android编程实现简单的UDP Client实例

本文实例讲述了Android编程实现简单的UDP Client.分享给大家供大家参考,具体如下: 该代码在4.2.2内调试通过 1.记得加权限 <uses-permission android:name="android.permission.INTERNET"/> 注意:Android 4.0之后,就不能在主线程进行socket通信,否则会抛异常. 2.代码 MainActivity.java: package mao.example.quicksend; import

Android编程实现文件浏览功能的方法【类似于FileDialog的功能】_Android

本文实例讲述了Android编程实现文件浏览功能的方法.分享给大家供大家参考,具体如下: 最近正在弄上传文件,当时想怎么能实现fileDialog的功能呢,打开文件,浏览文件,然后选择文件呢,查了好多资料,也看了不少论坛,都说里面没有这个功能,那真是奇怪了,里面没有这个功能,当然就需要自己动手添加这个功能了. 首先说一下这个文件浏览的简单实现原理: 首先选择一个目录做为根目录,然后打开此目录,常用的就是使用File这个类了,如下: File file=new File(path); 然后可以通过

Android编程实现播放MP3功能示例

本文实例讲述了Android编程实现播放MP3功能.分享给大家供大家参考,具体如下: 在android中播放mp3非常简单,也是项目中经常使用的,比如说要做项目的背景音乐,应用中某些功能的提示音等的.应用非常广泛,下面提供一个简单的使用实例: layout文件的配置: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.and

Android编程实现手机震动功能的方法

本文实例讲述了Android编程实现手机震动功能的方法.分享给大家供大家参考,具体如下: 在与用户交互时,常常会用到震动功能,以提醒用户.该功能实现比较简单,请参阅下面主要代码: import android.app.Activity; import android.app.Service; import android.os.Vibrator; public class TipHelper { public static void Vibrate(final Activity activity

Android编程模拟HOME键功能示例_Android

本文实例讲述了Android编程模拟HOME键功能的方法.分享给大家供大家参考,具体如下: 做一个类似于QQ按返回键并不销毁Activity的方法(即不调用Activity.finish(),系统不调用 onDestroy),而是类似于按Home键,让Activity类似于"暂停"(即只调用onPause,onDestroy). 代码如下: public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == Key

Android编程之简单计时器实现方法_Android

本文实例讲述了Android编程之简单计时器实现方法.分享给大家供大家参考,具体如下: 这里利用ContextMenu(上下文菜单),Chronometer实现简单计数器. Main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android