I need to get data usage statistics for last month.
Is there any way to calculate data usage date wise in android?
I used TrafficStats class for getting data usage but it gives me all data usage of device by the given UID since device boot.
I need to get data usage statistics for last month.
Is there any way to calculate data usage date wise in android?
I used TrafficStats class for getting data usage but it gives me all data usage of device by the given UID since device boot.
for those who are still looking, put this in 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();
}
}
then these methods in your activity, IMPORTANT in this case installedTime is the start time of month (long)
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: I just edited my code as per this requirement so it may have some syntext error