Android编程实现简单流量管理功能实例

 更新时间:2016年02月19日 09:05:39   作者:lg878398509  
这篇文章主要介绍了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.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程序设计有所帮助。

相关文章

  • Android 判断日期是否在一年以内的算法实例

    Android 判断日期是否在一年以内的算法实例

    下面小编就为大家带来一篇Android 判断日期是否在一年以内的算法实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Android实现列表时间轴

    Android实现列表时间轴

    这篇文章主要为大家详细介绍了Android实现列表时间轴效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • android实现滑动解锁

    android实现滑动解锁

    这篇文章主要为大家详细介绍了android实现滑动解锁,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • android非RxJava环境下使用Handler实现预加载

    android非RxJava环境下使用Handler实现预加载

    这篇文章主要介绍了android非RxJava环境下使用Handler实现预加载的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android ListView的OnItemClickListener详解

    Android ListView的OnItemClickListener详解

    这篇文章主要介绍了Android ListView的OnItemClickListener详解的相关资料,涉及到OnItemClickListener的position和id参数做详细的解释的知识点,非常不错,具有参考借鉴价值,需要的朋友参考下
    2016-07-07
  • Android 使用selector改变按钮状态实例详解

    Android 使用selector改变按钮状态实例详解

    这篇文章主要介绍了Android 使用selector改变按钮状态实例详解的相关资料,需要的朋友可以参考下
    2017-01-01
  • Android自定义View模仿即刻点赞数字切换效果实例

    Android自定义View模仿即刻点赞数字切换效果实例

    有一个项目是仿即刻的点赞,这篇文章主要给大家介绍了关于Android自定义View模仿即刻点赞数字切换效果的相关资料,文中通过示例代码介绍 的非常详细,需要的朋友可以参考下
    2022-12-12
  • Android编程实现动态支持多语言的方法

    Android编程实现动态支持多语言的方法

    这篇文章主要介绍了Android编程实现动态支持多语言的方法,涉及Android资源、控件及属性相关操作技巧,需要的朋友可以参考下
    2017-06-06
  • 深踩Android Studio 缓存的坑及解决方法

    深踩Android Studio 缓存的坑及解决方法

    这篇文章主要介绍了深踩Android Studio 缓存的坑及解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Android SeekBar在刷新使用中需要注意的问题

    Android SeekBar在刷新使用中需要注意的问题

    SeekBar在刷新使用中需要注意的问题:在使用SeekBar的过程中需要注意刷新频率,避免频繁刷新造成的性能问题;同时,需要对SeekBar的监听事件进行适当的优化,减少回调次数,提高响应速度
    2023-05-05

最新评论