0

I want to access the Data usage in android phone at application level.

In my phone with lollipop 5.1, it gives a beautiful graph of the data usage & settings to limit the usage. I think this feature came from Kitkat update.

I want this record, if there is no Public API, is there any other way to do that ?

Sanyam Jain
  • 2,925
  • 2
  • 23
  • 30

1 Answers1

0

First, to get the data, you should use either UsageStatsManager or ActivityManager, depending on which SDK version you're using. Here is a simple example to get the name of the currently running application:

        String currentApp = "NULL";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            UsageStatsManager usm = (UsageStatsManager) activity.getSystemService(Context.USAGE_STATS_SERVICE);
            long time = System.currentTimeMillis();
            List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,  time - 1000 * 1000, time);
            if ((appList != null) && (appList.size() > 0)) {
                SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
                for (UsageStats usageStats : appList) {
                    mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
                }
                if ((mySortedMap != null) && (!(mySortedMap.isEmpty()))) {
                    currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
                }
            }
        } else {
            ActivityManager activityManager = (ActivityManager) currentlyDisplayedScreen.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
            currentApp = services.get(0).topActivity.getPackageName().toString()
        }

These classes can be used to obtain other useful data as well, including how much time each app was in foreground. If you use this, you will need the following permissions:

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" />

I don't know if you can copy the usage statistics chart itself, but I've used Android's canvas to draw all sorts of stuff, including colorful charts displaying data of different categories. You can create a custom View which overrides the onDraw() method to draw your chart based on the current data you have:

public class StatisticsChartView extends View {
    ...
    @Override
    protected void onDraw(Canvas canvas) {
        if (values != null) {
            float sliceStartPoint = 200;
            path.addCircle(rectF.centerX(), rectF.centerY(), 250, Direction.CW);
            canvas.clipPath(path, Op.DIFFERENCE);
         ...
}

My assumption is that you want to have the chart inside your own application, but if you want you can simply launch the Android data usage screen of the device with a proper intent: Which Intent for Settings - Data usage However, then you're just navigating to an external screen.

Community
  • 1
  • 1
logcat
  • 283
  • 2
  • 12