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

时间:2021-09-27 23:48:30

本文实例讲述了android编程实现简单流量管理功能的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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程序设计有所帮助。