Android uses the Trafficstats class to get traffic for this app

Source: Internet
Author: User

 Public voidGetData () {//Packagemanager Package Management classPackagemanager Packagemanager = brownseractivity. This. Getpackagemanager (); intPackageuid = 0; BigDecimal Numrx=NewBigDecimal ("0"); BigDecimal NUMTX=NewBigDecimal ("0"); /*** Cycle through the package name of all apps and the current app's package name to match operations to save the obtained UID to a temporary variable*/        /*For (ApplicationInfo info:packageManager.getInstalledApplications (0)) {int uid = INFO.UID;            String PackageName = info.packagename;                if (Packagename.indexof ("Iassistant") >-1) {packageuid = UID;            Break }        }*/        //gets the UID of the current appPackageuid =Android.os.Process.myUid (); //The  Trafficstats class obtains data related to traffic based on the UID of the application        LongRx = trafficstats.getuidrxbytes (PACKAGEUID);//Total Receive Volume        Longtx = Trafficstats.getuidtxbytes (PACKAGEUID);//Total Send Volume        floatJustload =0f; LOG.I ("Scott", "This application this time the total receive volume:" +Rx); LOG.I ("Scott", "the total number of times this application is sent:" +tx); if(Rx > 0) {Numrx=bytetom (RX); Justload= Numrx.floatvalue ()-InitData; InitData=Numrx.floatvalue (); }        if(Tx > 0) {numtx=Bytetom (TX); }        //Toast.maketext (Brownseractivity.this, "Total Received:" +numrx.floatvalue () + "M",//toast.length_short). Show ();Toast.maketext (brownseractivity. This, "This receive Volume:" + justload + "M", Toast.length_short). Show (); LOG.I ("Scott", "Total received after conversion to (m):" +Numrx.floatvalue ()); LOG.I ("Scott", "total send amount after conversion to (m):" +Numtx.floatvalue ()); }    /*** bytes converted to M*/    PrivateBigDecimal Bytetom (Longvalue) {BigDecimal result=NewBigDecimal (long.tostring (value)); Result= Result.divide (NewBigDecimal ("1024.0")). Divide (NewBigDecimal ("1024.0")); returnresult; }

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.