4

I want to get diary use for all apps installed in phone, but always an empty list is returned. Below is the code i am using.

Here app.js from react-native call apps information from native code java

loadApps = async () => {
    await ReturnAppsInformations.getApps()
    .then(response => {
      console.log(response); // only [] <----
      this.setState({ apps });
    })
    .catch(error => {
      console.warn(error);
    });
}

Here is my simple native code to return array with data

UsageStatsManager manager = (UsageStatsManager) this.reactContext.getSystemService(this.reactContext.USAGE_STATS_SERVICE);
List<UsageStats> stats =manager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,1729196, System.currentTimeMillis());
WritableArray list2 = Arguments.createArray();

for (int i = 0; i < stats.size(); i++) {
    UsageStats usageStats = stats.get(i);
    WritableMap appInfo2 = Arguments.createMap();

    appInfo2.putString("packageName", usageStats.getPackageName());
    appInfo2.putDouble("firsTimeStamp", usageStats.getFirstTimeStamp());
    appInfo2.putDouble("getTotalTimeInForeground", 
    usageStats.getTotalTimeInForeground());
    list2.pushMap(appInfo2);
}
promise.resolve(list2);

What am I doing wrong? This is my first app so I do not have much knowledge

  1. Updated as suggested by Julien, but still results in a empty array.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.returnappsinformations" 
            xmlns:tools="http://schemas.android.com/tools">
        <uses-permission
            android:name="android.permission.PACKAGE_USAGE_STATS"
            tools:ignore="ProtectedPermissions" />
    </manifest>
    

Ok, i found those gits about it...

https://github.com/mvincent7891/UsageStatsModule

https://github.com/shimatai/react-native-android-datausage

https://github.com/lucasferreira/react-native-android-permissions

Tonight im gonna to read them... But i belive, that resolve the problem to me!! Thanks for support!!!

3 Answers3

5

Have you asked and set the necessary permissions to use UsageStatsManager?

UsageStatsManager's documentation states that you need to declare android.permission.PACKAGE_USAGE_STATS in your Manifest and that it needs to be turned on in the Settings of your phone for your particular application.

EDIT:
Permission to add in your Manifest:

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />

That special permission can't be granted through the normal permissions API of Android. So you'll need to redirect your users to the Settings page where they can grant it manually. You can open the right Settings screen via the Intent:

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
Julien Arzul
  • 961
  • 9
  • 7
  • Thanks Julien, ill do it and and I answer you!! – Camilo Melges Jan 23 '19 at 01:46
  • Hey, inserted android manifest, but only still with empty response :( – Camilo Melges Jan 23 '19 at 03:34
  • 2
    You also need to accept the permission on your Phone. Since this is a "special" permission, there is no way to ask for it programmatically. You'll need to go to Settings => Apps & notifications => Advanced => Special app access => Usage Access (that's on a Pixel phone, it might be somewhere else on other manufacturer Settings apps) – Julien Arzul Jan 23 '19 at 06:19
  • (and sorry, I confused the permission with the Settings constant, I corrected it in my original answer) – Julien Arzul Jan 23 '19 at 06:22
  • Oh my, so is there no way to do this automatically? – Camilo Melges Jan 23 '19 at 12:03
  • I don't believe so. You can have a look at the app ActionDash that is doing what you want with a nice onboarding flow: https://play.google.com/store/apps/details?id=com.actiondash.playstore – Julien Arzul Jan 24 '19 at 05:39
  • Now im using a background task to save in DB the data of days... im creating a same app for different reasons :p Julien Arzul – Camilo Melges Feb 15 '19 at 19:05
  • https://facebook.github.io/react-native/docs/headless-js-android resolve this!! But only for android, for while... – Camilo Melges Apr 20 '19 at 14:48
1

you need to declare android.permission.PACKAGE_USAGE_STATS in your Manifest

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

as well as check at run time

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.PACKAGE_USAGE_STATS)
        != PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted
}

for more check How to use UsageStatsManager?

Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
0

As @JulienArzul pointed you need to add permission android.settings.USAGE_ACCESS_SETTINGS and then need to check whether you have it or not using below code:-

AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOps.checkOpNoThrow(OPSTR_GET_USAGE_STATS, myUid(), context.getPackageName());
return mode == MODE_ALLOWED;

If above condition returns false, redirect user to that permission's settings screen

Intent intent = new Intent(MainActivity.this, AppLockService.class);
intent.setAction(AppLockService.ACTION_START_FOREGROUND_SERVICE);
startService(intent);
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41