如何在安卓中获取过去 1 个月的数据使用情况



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

有没有办法在安卓中计算数据使用日期?

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

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

@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();
    }
}

然后这些方法在你的活动中,重要提示在本例中安装时间是月份的开始时间(长)

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:我刚刚按照此要求编辑了我的代码,因此它可能有一些合成错误

相关内容

  • 没有找到相关文章

最新更新