Query the traffic information of an application that can be started

Source: Internet
Author: User

1. traffic_manager_item.xml

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <ImageView android: id = "@ + id/iv_appicon" android: layout_width = "35dip" android: layout_height = "35dip" android: scaleType = "fitXY" android: src = "@ drawable/ic_launcher"/> <TextView android: id = "@ + id/TV _appname" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_toRightOf = "@ id/iv_appicon" android: text = "My most swing" android: textColor = "@ android: color/white" android: textSize = "20sp"/> <TextView android: id = "@ + id/TV _apptarffic" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: text = "20 MB" android: textColor = "@ android: color/white" android: textSize = "20sp"/> <LinearLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ id/TV _appname" android: layout_toRightOf = "@ id/iv_appicon" android: orientation = "horizontal"> <TextView android: id = "@ + id/TV _apptx" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginRight = "80dp" android: text = "upload: 20 MB "android: textSize =" 14sp "/> <TextView android: id =" @ + id/TV _apprx "android: layout_width =" wrap_content "android: layout_height = "wrap_content" android: text = "Download: 20 MB" android: textSize = "14sp"/> </LinearLayout> </RelativeLayout>


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@android:color/background_dark"    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="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:gravity="center"                android:text="2g/3g" />            <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:gravity="center"                android:text="wifi" />        </TableRow>        <TableRow            android:layout_width="fill_parent"            android:layout_height="wrap_content" >            <TextView                android:id="@+id/tv_traffic_manager_mobile"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:gravity="center"                android:text="200Mb" />            <TextView                android:id="@+id/tv_traffic_manager_wifi"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:gravity="center"                android:text="50MB" />        </TableRow>    </TableLayout>    <SlidingDrawer        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:content="@+id/lv_traffic_manager_content"        android:handle="@+id/handle"        android:orientation="vertical" >        <ImageView            android:id="@id/handle"            android:layout_width="30dp"            android:layout_height="30dp"            android:src="@drawable/notification" />        <ListView            android:id="@id/lv_traffic_manager_content"            android:layout_width="fill_parent"            android:layout_height="fill_parent" >        </ListView>    </SlidingDrawer></LinearLayout>


Package com. njupt. testtracfficstate; import java. util. arrayList; import java. util. list; import android.net. trafficStats; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; import android. content. context; import android. view. layoutInflater; import android. view. menu; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; Import android. widget. imageView; import android. widget. listView; import android. widget. textView; public class MainActivity extends Activity {protected static final int SUCCESS_GET_TRAFFICINFO = 0;/** Called when the activity is first created. */private TextView TV _traffic_manager_mobile; private TextView TV _traffic_manager_wifi; private ListView lv_traffic_manager_content; private TrafficManagerService TrafficManagerService; private List <TrafficInfo> trafficInfos; private List <TrafficInfo> realTrafficInfos; private TrafficManagerAdapter mAdapter; private Handler mHandler = new Handler () {public void handleMessage (android. OS. message msg) {switch (msg. what) {case SUCCESS_GET_TRAFFICINFO: mAdapter = new TrafficManagerAdapter (getApplicationContext (); lv_traffic_manager_content.setAdapter (mAdapter); break; Default: break ;}};}; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main);/* 2g3g receives traffic TrafficStats. getcyclerxbytes (); // The packet TrafficStats received by 2g/3g. getcyclerxpackets (); // traffic TrafficStats for 2g/3g uploads. getMobileTxBytes (); // 2g/3g uploaded packet TrafficStats. getMobileTxPackets (); // The total traffic received by the mobile phone TrafficStats. getTotalRxBytes (); // total traffic uploaded by the mobile phone Traffi CStats. getTotalTxBytes (); // get the traffic TrafficStats received by the application. getUidRxBytes (uid); TrafficStats. getUidTxBytes (uid); */TV _traffic_manager_mobile = (TextView) findViewById (R. id. TV _traffic_manager_mobile); TV _traffic_manager_wifi = (TextView) findViewById (R. id. TV _traffic_manager_wifi); lv_traffic_manager_content = (ListView) findViewById (R. id. lv_traffic_manager_content); TV _traffic_manager_mobile.setText (Tex TFormat. formatByte (getMobileTotal (); TV _traffic_manager_wifi.setText (TextFormat. formatByte (getWifiTotal (); trafficManagerService = new TrafficManagerService (this); new Thread () {public void run () {trafficInfos = trafficManagerService. getLauncherTrafficInfos (); realTrafficInfos = new ArrayList <TrafficInfo> (); for (TrafficInfo info: trafficInfos) {if (TrafficStats. getUidRxBytes (info. getUid () =- 1 & TrafficStats. getUidTxBytes (info. getUid () =-1) {//} else {realTrafficInfos. add (info) ;}} Message msg = new Message (); msg. what = SUCCESS_GET_TRAFFICINFO; mHandler. sendMessage (msg );};}. start ();}/*** total 2g/3g traffic * @ return */private long getMobileTotal () {long mobile_rx = TrafficStats. getMobileRxBytes (); long mobile_tx = TrafficStats. getMobileTxBytes (); return mobile_rx + mobile_tx ;}/** * Get the total traffic of the mobile phone * @ return */private long getTotal () {return TrafficStats. getTotalRxBytes () + TrafficStats. getTotalTxBytes ();}/*** get the total traffic of wifi * @ return */private long getWifiTotal () {return getTotal ()-getMobileTotal ();} static class ViewHolder {ImageView iv_appicon; TextView TV _appname; TextView TV _apptraffic; TextView TV _apptx; TextView TV _apprx;} private final class trafficmanagerext Adapter Ends BaseAdapter {private LayoutInflater mInflater; public TrafficManagerAdapter (Context context) {mInflater = LayoutInflater. from (context);} public int getCount () {return realTrafficInfos. size ();} public Object getItem (int position) {return realTrafficInfos. get (position);} public long getItemId (int position) {return position;} public View getView (int position, View convertView, ViewGroup parent) {Vi Ew view = null; ViewHolder holder = null; if (convertView! = Null) {view = convertView; holder = (ViewHolder) view. getTag ();} else {view = mInflater. inflate (R. layout. traffic_manager_item, null); holder = new ViewHolder (); holder. iv_appicon = (ImageView) view. findViewById (R. id. iv_appicon); holder. TV _appname = (TextView) view. findViewById (R. id. TV _appname); holder. TV _apptraffic = (TextView) view. findViewById (R. id. TV _apptarffic); holder. TV _apptx = (TextView) view. findViewById (R. id. TV _apptx); holder. TV _apprx = (TextView) view. findViewById (R. id. TV _apprx); view. setTag (holder);} TrafficInfo trafficInfo = realTrafficInfos. get (position); holder. iv_appicon.setImageDrawable (trafficInfo. getAppicon (); holder. TV _appname.setText (trafficInfo. getAppname (); int uid = trafficInfo. getUid (); long tx = TrafficStats. getUidTxBytes (uid); if (tx =-1) {tx = 0;} long rx = TrafficStats. getUidRxBytes (uid); if (rx =-1) {rx = 0;} long total = tx + rx; holder. TV _apptraffic.setText (TextFormat. formatByte (total); holder. TV _apptx.setText ("upload:" + TextFormat. formatByte (tx); holder. TV _apprx.setText ("Download:" + TextFormat. formatByte (rx); return view; }}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}


Package com. njupt. testtracfficstate; import java. util. arrayList; import java. util. list; import android. content. context; import android. content. intent; import android. content. pm. applicationInfo; import android. content. pm. packageManager; import android. content. pm. resolveInfo; import android. graphics. drawable. drawable; public class TrafficManagerService {private Context context; private PackageManager pm; public TrafficManagerService (Context context) {this. context = context; pm = context. getPackageManager ();}/*** get all started Applications * @ return */public List <TrafficInfo> getLauncherTrafficInfos () {List <TrafficInfo> trafficInfos = new ArrayList <TrafficInfo> (); // query the applications that can be started. Intent intent = new Intent (); intent. setAction (Intent. ACTION_MAIN); intent. addCategory (Intent. CATEGORY_LAUNCHER); // ResolveInfo is similar to an IntentFilterList <ResolveInfo> resolveInfos = pm. queryIntentActivities (intent, PackageManager. MATCH_DEFAULT_ONLY); for (ResolveInfo info: resolveInfos) {ApplicationInfo appInfo = info. activityInfo. applicationInfo; Drawable appicon = appInfo. loadIcon (pm); String appname = appInfo. loadLabel (pm ). toString (); String packageName = appInfo. packageName; int uid = appInfo. uid; trafficInfos. add (new TrafficInfo (appicon, appname, packageName, uid);} return trafficInfos ;}}


package com.njupt.testtracfficstate;import android.graphics.drawable.Drawable;public class TrafficInfo {private Drawable appicon;private String appname;private String packageName;private int uid;public TrafficInfo() {super();}public TrafficInfo(Drawable appicon, String appname, String packageName,int uid) {super();this.appicon = appicon;this.appname = appname;this.packageName = packageName;this.uid = uid;}public Drawable getAppicon() {return appicon;}public void setAppicon(Drawable appicon) {this.appicon = appicon;}public String getAppname() {return appname;}public void setAppname(String appname) {this.appname = appname;}public String getPackageName() {return packageName;}public void setPackageName(String packageName) {this.packageName = packageName;}public int getUid() {return uid;}public void setUid(int uid) {this.uid = uid;}@Overridepublic String toString() {return "TrafficInfo [appicon=" + appicon + ", appname=" + appname+ ", packageName=" + packageName + ", uid=" + uid + "]";}}


Package com. njupt. testtracfficstate; import java. text. decimalFormat; public class TextFormat {// 23.87 MB 00.87 MB/KB bytepublic static String formatByte (long data) {DecimalFormat format = new DecimalFormat ("##. # "); if (data <1024) {return data +" bytes ";} else if (data <1024*1024) {return format. format (data/1024f) + "KB";} else if (data <1024*1024*1024) {return format. format (data/1024f/1024f) + "MB";} else if (data <1024*1024*1024*1024) {return format. format (data/1024f/1024f/1024f) + "GB" ;}else {return "return beyond statistics returned ";}}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.