My Phone Butler (12) Network assistant logic code

Source: Internet
Author: User

My Phone Butler (12) Network assistant logic code

public class

public class Trafficstats extends Object

  A class that provides network traffic statistics. These statistics include bytes sent and received packets sent and received by the network,

On all interfaces, on the phone interface, and on a per user basis.
These statistics may not be available on all platforms. If the device does not support statistical data, it will UNSUPPORTED be returned.

receiving and sending Packets

Static long GetmobilerxPackets()

The number of returned packets received from the entire mobile network since the device started.

Static long getmobiletxpackets ()

Send

Number of bytes received and sent

Static Long Getmobilerxbytes ()

Receive

Static long getmobiletxbytes ()

The number of bytes transferred in the mobile network since the boot device started.

The number of bytes received and sent by the application of the given UID

  getuidrxbytes (int uid) receive

getuidtxbytes (int uid) send

    /*** Get information about using traffic*/    Private FinalRunnable Runnable =NewRunnable () { Public voidrun () {FinalPackagemanager pm = networkhelperactivity. This. Getpackagemanager (); ArrayList<ResolveInfo> trafficlists =NewArraylist<resolveinfo>(); Intent Intent=NewIntent (); Intent.setaction ("Android.intent.action.MAIN"); Intent.addcategory ("Android.intent.category.LAUNCHER"); Finallist<resolveinfo> lists = pm.queryintentactivities (Intent, 0);  for(ResolveInfo info:lists) {if(Trafficstats.getuidrxbytes (info.activityInfo.applicationInfo.uid)! =-1 && trafficstats.getuidtxb Ytes (info.activityInfo.applicationInfo.uid)! =-1) {Trafficlists.add (info); }            }            //initialize the adapter, and bindadapter =NewMynetworkadapter (networkhelperactivity. This, trafficlists);        Lv.setadapter (adapter); }            } ;

Logic code:

 Packagecom.chb.myphonesave.activity;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Timer;ImportJava.util.TimerTask;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.content.pm.PackageManager;ImportAndroid.content.pm.ResolveInfo;Importandroid.net.TrafficStats;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.Window;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;ImportCOM.CHB.MYPHONESAVE.R;ImportCom.chb.myphonesave.adapter.MyNetworkAdapter;/*** Network Assistant*/ Public classNetworkhelperactivityextendsActivity {PrivateListView LV; PrivateMynetworkadapter Adapter; PrivateTimer timer; PrivateHandler Handler =NewHandler () { Public voidhandlemessage (Message msg) {Super. Handlemessage (msg);        Adapter.notifydatasetchanged ();     }              }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.activity_network_helper); LV=(ListView) Findviewbyid (r.id.nt_lv_content); TextView Tvgprs=(TextView) Findviewbyid (R.ID.GPRS); TextView Tvwifi=(TextView) Findviewbyid (R.id.wifi); Tvgprs.settext (Trafficstats.getmobilerxpackets ()+ trafficstats.getmobiletxpackets () + "package"); Tvwifi.settext (Trafficstats.gettotalrxpackets ()+ trafficstats.gettotaltxpackets () + "package"); } @Overrideprotected voidOnStart () {//TODO auto-generated Method Stub        Super. OnStart (); TimerTask Task=NewTimerTask () {@Override Public voidrun () {Message msg=NewMessage ();            Handler.sendmessage (msg);    }        }; } @Overrideprotected voidOnresume () {//TODO auto-generated Method Stub        Super. Onresume (); //if the thread is running in the UI interface, otherwise, it is added to the UI interface in the time queueRunonuithread (runnable); } @Overrideprotected voidOnStop () {//TODO auto-generated Method Stub        Super. OnStop ();    Timer.cancel (); }            /*** Get information about using traffic*/    Private FinalRunnable Runnable =NewRunnable () { Public voidrun () {FinalPackagemanager pm = networkhelperactivity. This. Getpackagemanager (); ArrayList<ResolveInfo> trafficlists =NewArraylist<resolveinfo>(); Intent Intent=NewIntent (); Intent.setaction ("Android.intent.action.MAIN"); Intent.addcategory ("Android.intent.category.LAUNCHER"); Finallist<resolveinfo> lists = pm.queryintentactivities (Intent, 0);  for(ResolveInfo info:lists) {if(Trafficstats.getuidrxbytes (info.activityInfo.applicationInfo.uid)! =-1 && trafficstats.getuidtxb Ytes (info.activityInfo.applicationInfo.uid)! =-1) {Trafficlists.add (info); }            }            //initialize the adapter, and bindadapter =NewMynetworkadapter (networkhelperactivity. This, trafficlists);        Lv.setadapter (adapter); }            } ;}

My Phone Butler (12) Network assistant logic code

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.