如何在android中获取最近1个月的数据使用情况?

时间:2016-03-10 05:40:54

标签: android android-data-usage

我需要获取上个月的数据使用情况统计数据。

有没有办法在android中明智地计算数据使用日期?

我使用 TrafficStats 类来获取数据,但它提供了设备启动后给定UID对设备的所有数据使用情况。

1 个答案:

答案 0 :(得分:0)

对于那些仍在寻找的人,请将其放在 onCreate 中。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    installedTime = getInstalledTime();
    if (checkUserStatePermission()) {
        startActivityForResult(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS), REQUEST_USAGE_PERMISSION);

    } else {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
                
       ) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE
                    }, REQUEST_PHONE_STATE);
        }
        else getInternetUsage();
    }
}

然后在您的活动中使用这些方法, 重要在这种情况下,installedTime 是一个月的开始时间(长)

private void getInternetUsage() {
    NetworkStatsManager networkStatsManager = (NetworkStatsManager) getSystemService(Context.NETWORK_STATS_SERVICE);
    NetworkStats.Bucket bucketWifi, bucketMobile;

    try {

        bucketWifi = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_WIFI,
                "",
                installedTime,
                System.currentTimeMillis());

        bucketMobile = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_MOBILE,
                getSubscriberId(),
                installedTime,
                System.currentTimeMillis());


        Log.d("MOBILE INTERNET UP", bucketMobile.getTxBytes() / (1024f * 1024f) + " MB");
        Log.d("MOBILE INTERNET DOWN", bucketMobile.getRxBytes() / (1024f * 1024f) + " MB");


        Log.d("WIFI INTERNET UP", bucketWifi.getTxBytes() / (1024f * 1024f) + " MB");
        Log.d("WIFI INTERNET DOWN", bucketWifi.getRxBytes() / (1024f * 1024f) + " MB");


    } catch (RemoteException e) {
        e.printStackTrace();
    }
}


/*
we can pass null above Q
 */
@SuppressLint({"MissingPermission", "HardwareIds"})
public String getSubscriberId() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getSubscriberId();
    } else {
        return null;
    }
}
private boolean checkUserStatePermission() {
    AppOpsManager appOps = (AppOpsManager)
            getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOps.checkOpNoThrow("android:get_usage_stats",
            android.os.Process.myUid(), getPackageName());
    return mode != AppOpsManager.MODE_ALLOWED;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_USAGE_PERMISSION) {
        if (checkUserStatePermission()) {
            Toast.makeText(this, "permission denied", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            getInternetUsage();
        }

    }
}

PS:我只是按照这个要求编辑了我的代码,所以它可能有一些语法错误