欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-20 23:50:58
本文实例讲述了android编程实现简单流量管理功能的方法。分享给大家供大家参考,具体如下: package cn.itcast.mobilesafe.ui;...

本文实例讲述了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程序设计有所帮助。