5

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

有什么方法可以在android中明智地计算数据使用日期吗?

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

4

1 回答 1

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:我只是按照这个要求编辑了我的代码,所以它可能有一些语法错误

于 2021-07-16T06:39:20.120 回答