First look at the interface
Layout files are very simple, do not post code! Stick out the main code
Each of the different programs can have only one UID, but one application may have multiple PID, so we get the UID of each application
Therefore, we use the AppInfo entity class and the Appinfos logic processing class inside the software management to add the UID field, can satisfy our demand.
to Appinfo.ja VA Join
Private int uid; Public int Getuid () { return uid; } Public void setUid (int uid) { this. uid = uid; }
Join in Appinfos.java
int uid = installPackage.applicationInfo.uid;
Appinfo.setuid (UID);
Trafficmanageractivity. java
Public classTrafficmanageractivityextendsActivity {Privatelist<AppInfo>Appinfos; PrivateListView LV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Initui (); InitData (); } Private voidInitData () {//TODO auto-generated Method Stub NewThread () { Public voidrun () {//get a collection of all applicationsAppinfos = Appinfos. Getappinfos (trafficmanageractivity. This); System.out.println ("Get collection of all applications list ..."); //Message msg = new Message ();//hanlder.sendmessage (msg);Runonuithread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubMyadapter adapter =NewMyadapter (); Lv.setadapter (adapter); } }); }; }.start (); } Private voidInitui () {//TODO auto-generated Method StubSetcontentview (r.layout. Activity_traffic); LV=(ListView) Findviewbyid (r.id.lv); } //private Handler Hanlder = new Handler () {//Public void Handlemessage (Android.os.Message msg) {//Myadapter adapter = new Myadapter ();//Lv.setadapter (adapter);// // }; // }; Public classMyadapterextendsbaseadapter{@Override Public intGetCount () {//TODO auto-generated Method Stub returnappinfos.size (); } @Override PublicObject GetItem (intposition) { returnAppinfos.get (position); } @Override Public LongGetitemid (intposition) { //TODO auto-generated Method Stub returnposition; } @Override PublicView GetView (intpoition, View Convertview, ViewGroup parent) { //TODO auto-generated Method StubViewholder Holder; if(convertview!=NULL) {Holder=(Viewholder) Convertview.gettag (); }Else{Convertview= Layoutinflater.from (trafficmanageractivity. This). Inflate (r.layout.item_traffic,NULL); Holder=NewViewholder (); Holder.icon=(ImageView) Convertview.findviewbyid (R.id.iv_icon); Holder.appname=(TextView) Convertview.findviewbyid (r.id.tv_appname); Holder.all=(TextView) Convertview.findviewbyid (R.id.tv_all); Holder.rx=(TextView) Convertview.findviewbyid (R.ID.TV_RX); Holder.tx=(TextView) Convertview.findviewbyid (R.ID.TV_TX); Convertview.settag (holder); } AppInfo Info=Appinfos.get (poition); Holder.icon.setImageDrawable (Info.geticon ()); Holder.appname.setText (Info.getapkname ()); intUID =Info.getuid (); System.out.println ("Unique Indication:" +uid); Longtx = trafficstats.getuidtxbytes (UID); System.out.println ("Download:" +tx); if(tx<0) {TX= 0; } LongRx = Trafficstats.getuidrxbytes (UID); if(rx<0) {Rx= 0; } LongTotal = tx+Rx; Holder.rx.setText ("Download:" + TextFormat. Formatbyte (RX)); Holder.tx.setText ("Upload:" +Textformat.formatbyte (TX)); Holder.all.setText ("Total Traffic:" +textformat.formatbyte (total)); returnConvertview; } } Public classviewholder{ImageView icon; TextView appname; TextView TX; TextView Rx; TextView all; } }
Textformat.java
ImportJava.text.DecimalFormat; Public class TextFormat {/*** Formatted data *@paramData *@return */ Public StaticString Formatbyte (Longdata) {DecimalFormat format=NewDecimalFormat ("##.##"); if(Data < 1024){ returndata+ "bytes"; }Else if(Data < 1024 * 1024){ returnFormat.format (data/1024f) + "KB"; }Else if(Data < 1024 * 1024 * 1024){ returnFormat.format (data/1024f/1024f) + "MB"; }Else if(Data < 1024 * 1024 * 1024 * 1024){ returnFormat.format (data/1024f/1024f/1024f) + "GB"; }Else{ return"Beyond the Statistical range"; } } }
Mobile security Defender--traffic management