Mayuri Ruparel
Mayuri Ruparel

Reputation: 694

How to get Data usage seperate by wifi and mobile networks Android?

I'm tying to get usage of data till date using Wi-Fi & Mobile Networks, I've tried a lot but I've not got any code for that.I've got this code but its also giving me '0' received & sent bytes.

mStartRX = TrafficStats.getMobileTxBytes();
mStartTX = TrafficStats.getMobileTxBytes();

This is I do not need. I want to calculate both Mobile Network and Wi-Fi separately.

Upvotes: 2

Views: 2206

Answers (2)

Lucifer
Lucifer

Reputation: 29662

I have worked on such requirement where I am storing the data usage ( Mobile & WiFi ) by a specific application.

BroadcastReceiver in Manifest.xml

<receiver android:name="com.example.datausage.BrodcastNetwork" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" >
        </action>
    </intent-filter>
</receiver>

Permissions in Manifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

BroadcastReceiver's Code

public class BrodcastNetwork extends BroadcastReceiver 
{

    public static final int CONNECTION_WIFI = 2, CONNECTION_MOBILE = 1,
            CONNECTION_NOT = 0;
    private MyPreferences preferences;
    private DataUsageHelper dataUsageHelper;
    private long currentTime = 0; 

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        currentTime = new Date().getTime();
        preferences = new MyPreferences(context);
        dataUsageHelper = new DataUsageHelper(context);

        if ( ( currentTime - preferences.getSaveUsageTime() ) > 3000 )
        {
            new ChangeStatusThread( context ).start();
        }
    }

    private class ChangeStatusThread extends Thread
    {
        private Context context; 

        public ChangeStatusThread ( Context context )
        {
            this.context = context;
        }

        @Override
        public void run()
        {
            // Saving the previous usage before changing the status 
            dataUsageHelper.saveAllDataUsage();
            if (isNetworkConnected(context)) 
            {
                System.out.println  ( "Connected" );
                if (isWifi(context))
                    preferences.setCurrentConnection(CONNECTION_WIFI);
                else
                    preferences.setCurrentConnection(CONNECTION_MOBILE);
            } 
            else 
            {
                System.out.println  ( "Disconnected" );
                Log.e("onRecDataUsage", "DisConnected");
                preferences.setCurrentConnection(CONNECTION_NOT);
            }
        }
    }

    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    }

    public static boolean isWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
    }
}

Upvotes: 1

Manish Dubey
Manish Dubey

Reputation: 4306

Try below code:

final PackageManager pm = getPackageManager();
        //get a list of installed apps.
        List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
        int UID = 0;
        //loop through the list of installed packages and see if the selected
        //app is in the list
        for (ApplicationInfo packageInfo : packages) 
        {
            if(packageInfo.packageName.equals("com.example.myapp"))
            {
                //get the UID for the selected app
                UID = packageInfo.uid;
            }
           //Do whatever with the UID
           //Log.i("Check UID", "UID is: " + UID);
        }

        long rx = TrafficStats.getUidRxBytes(UID);
        long tx = TrafficStats.getUidTxBytes(UID);
        //Log.v(UserProfile.class.getName(), "Rx : "+rx+" Tx : "+tx);

        Log.v("TAG", "Rec. Bytes : "+rx/1000+" KB");
        Log.v("TAG", "Sent Bytes : "+tx/1000+" KB");

    }

Upvotes: 0

Related Questions