import android.annotation.TargetApi; import android.app.usage.NetworkStats; import android.app.usage.NetworkStatsManager; import android.content.Context; import android.net.ConnectivityManager; import android.os.Build; import android.os.RemoteException; import android.telephony.TelephonyManager; import android.util.Log; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created by Robert Zagórski on 2016-09-09. */ @TargetApi(Build.VERSION_CODES.M) public class NetworkStatsHelper { NetworkStatsManager networkStatsManager; int packageUid; public NetworkStatsHelper(NetworkStatsManager networkStatsManager) { this.networkStatsManager = networkStatsManager; } public NetworkStatsHelper(NetworkStatsManager networkStatsManager, int packageUid) { this.networkStatsManager = networkStatsManager; this.packageUid = packageUid; } public long getAllTodayMobile(Context context) { NetworkStats.Bucket bucket; try { bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_MOBILE, getSubscriberId(context, ConnectivityManager.TYPE_MOBILE), getTimesmorning(), System.currentTimeMillis()); } catch (RemoteException e) { return -1; } return bucket.getTxBytes() + bucket.getRxBytes(); } public long getAllMonthMobile(Context context) { NetworkStats.Bucket bucket; try { bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_MOBILE, getSubscriberId(context, ConnectivityManager.TYPE_MOBILE), getTimesMonthmorning(), System.currentTimeMillis()); } catch (RemoteException e) { return -1; } return bucket.getRxBytes() + bucket.getTxBytes(); } public long getAllRxBytesWifi() { NetworkStats.Bucket bucket; try { bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_WIFI, "", 0, System.currentTimeMillis()); } catch (RemoteException e) { return -1; } return bucket.getRxBytes(); } public long getAllTxBytesWifi() { NetworkStats.Bucket bucket; try { bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_WIFI, "", 0, System.currentTimeMillis()); } catch (RemoteException e) { return -1; } return bucket.getTxBytes(); } public long getPackageRxBytesMobile(Context context) { NetworkStats networkStats = null; try { networkStats = networkStatsManager.queryDetailsForUid( ConnectivityManager.TYPE_MOBILE, getSubscriberId(context, ConnectivityManager.TYPE_MOBILE), 0, System.currentTimeMillis(), packageUid); } catch (RemoteException e) { return -1; } NetworkStats.Bucket bucket = new NetworkStats.Bucket(); networkStats.getNextBucket(bucket); networkStats.getNextBucket(bucket); return bucket.getRxBytes(); } public long getPackageTxBytesMobile(Context context) { NetworkStats networkStats = null; try { networkStats = networkStatsManager.queryDetailsForUid( ConnectivityManager.TYPE_MOBILE, getSubscriberId(context, ConnectivityManager.TYPE_MOBILE), 0, System.currentTimeMillis(), packageUid); } catch (RemoteException e) { return -1; } NetworkStats.Bucket bucket = new NetworkStats.Bucket(); networkStats.getNextBucket(bucket); return bucket.getTxBytes(); } public long getPackageRxBytesWifi() { NetworkStats networkStats = null; try { networkStats = networkStatsManager.queryDetailsForUid( ConnectivityManager.TYPE_WIFI, "", 0, System.currentTimeMillis(), packageUid); } catch (RemoteException e) { return -1; } NetworkStats.Bucket bucket = new NetworkStats.Bucket(); networkStats.getNextBucket(bucket); return bucket.getRxBytes(); } public long getPackageTxBytesWifi() { NetworkStats networkStats = null; try { networkStats = networkStatsManager.queryDetailsForUid( ConnectivityManager.TYPE_WIFI, "", 0, System.currentTimeMillis(), packageUid); } catch (RemoteException e) { return -1; } NetworkStats.Bucket bucket = new NetworkStats.Bucket(); networkStats.getNextBucket(bucket); return bucket.getTxBytes(); } private String getSubscriberId(Context context, int networkType) { if (ConnectivityManager.TYPE_MOBILE == networkType) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); // final String actualSubscriberId = tm.getSubscriberId(); // return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId); return tm.getSubscriberId(); } return ""; } /** * 获取当天的零点时间 * * @return */ public static long getTimesmorning() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); return (cal.getTimeInMillis()); } //获得本月第一天0点时间 public static int getTimesMonthmorning() { Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH)); return (int) (cal.getTimeInMillis()); } }
获取当天的流量值跟手机获取的值是一致的,但当月总流量就会有偏差,在这份代码上得到的值会比手机系统统计的多好多。