Android programming to implement simple traffic management function Example _android

Source: Internet
Author: User
Tags static class

The examples in this article describe the way Android programming implements simple traffic management functionality. Share to everyone for your reference, specific as follows:

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" &
  Gt
      <tablelayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <TableRow Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" > <textview android:la Yout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "2G/ 3G total Flow "/> <textview android:id=" @+id/_3gtotal "android:layout_width=" 0dip "Android:lay out_height= "Wrap_content" android:layout_weight= "1"/> </TableRow> <tablerow Android:lay Out_width= "Fill_parent" android:layout_height= "wrap_content" > <textview android:layout_width= "0 Dip "Android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "WiFi Total traffic"/> <textview and Roid:id= "@+id/wifitotal" android:layout_width= "0dip" android:layout_height= "Wrap_content" Android: layout_weight= "1"/> </TableRow> </TableLayout> <slidingdrawer android:id= "@+id/ll_sd_traffi
    C "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_heig

 ht= "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_launche R "/> <textview android:id=" @+id/tv_traffic_name "android:layout_width=" 0dip "android:layout_height=" W Rap_content "android:layout_weight=" 1 "android:gravity=" Center_horizontal "android:text=" name "/> <TextV Iew android:id= "@+id/tv_traffic_tx" android:layout_width= "0dip" android:layout_height= "Wrap_content" Andro id:layout_weight= "1" android:gravity= "center_horizontal" android:text= "Upload/> <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= "Download"/> </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= "icon"/> Xtview android:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" android: Gravity= "Center_horizontal" android:text= "name"/> <textview android:layout_width= "0dip" Android:layout_
  height= "Wrap_content" android:layout_weight= "1" android:gravity= "center_horizontal" android:text= "Upload"/> <textview android:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" an Droid:gravity= "Center_horizontal" Android:text= "Download"/> </LinearLayout>

 

More interested readers of Android-related content can view this site: "Introduction to Android Development and advanced Course", "Android Communication Summary", "Android Basic Components Usage Summary", "Android View Summary", " Android Layout layout Tips and a summary of the use of Android controls

I hope this article will help you with the Android program.

Related Article

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.