如何通过WiFi和移动网络Android分离数据?

时间:2014-05-05 06:39:59

标签: android android-wifi

我想使用Wi-Fi& amp;移动网络,我已经尝试了很多,但我没有得到任何代码。我已经获得了此代码,但它也给了我'0' received & sent bytes

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

这是我不需要的。我想分别计算移动网络和Wi-Fi。

2 个答案:

答案 0 :(得分:1)

我一直致力于这样的要求,即我通过特定应用程序存储数据使用情况(Mobile& WiFi)。

Manifest.xml中的BroadcastReceiver

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

Manifest.xml中的权限

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

BroadcastReceiver的代码

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;
    }
}

答案 1 :(得分:0)

尝试以下代码:

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");

    }