B4A Library UsageStatsManager

This is a wrap for androids UsageStatsManager

UsageStatsManager
Author:
DonManfred (wrapper)
Version: 1
  • UsageStats
    Methods:
    • Initialize (usageStats As UsageStats)
    • IsInitialized As Boolean
    Properties:
    • FirstTimeStamp As Long [read only]
    • LastTimeStamp As Long [read only]
    • LastTimeUsed As Long [read only]
    • PackageName As String [read only]
    • TotalTimeInForeground As Long [read only]
    • describeContents As Int [read only]
  • UsageStatsManager
    Methods:
    • Initialize (EventName As String)
    • IsInitialized As Boolean
    • queryAndAggregateUsageStats (start As Long, end As Long) As Map
    • queryUsageStats (start As Long, end As Long) As List


Sample code

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim usm As UsageStatsManager
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    usm.Initialize("")
    Log(DateTime.Now)
   
    Dim starttime As Long
    starttime = DateTime.DateParse("10/20/2016")
    Dim endtime As Long
    endtime = DateTime.Now
   
   
    Dim usage1 As List = usm.queryUsageStats(starttime,endtime)
    For i = 0 To usage1.Size-1
        Dim us As UsageStats = usage1.Get(i)
        Log(us)
    Next

    Dim usage2 As Map = usm.queryAndAggregateUsageStats(starttime,endtime)
    Log(usage2)
End Sub

- Please note the Permission inside Manifest!
- Please note that you need to Add you App to the Exceptionlist in your Android Security settings.
Settings/Security/Allow app use of Usage stats
Add your app to the list...
 

Attachments

  • USMEx.zip
    7 KB · Views: 543
  • UsageStatsManagerV1.0.zip
    3.6 KB · Views: 569

Rusty

Well-Known Member
Licensed User
Longtime User
Hi Don,
I have tried to use your wrap and it gives me:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 342)
java.lang.NoClassDefFoundError: android.app.usage.UsageStatsManager
at de.donmanfred.UsageStatsManagerWrapper.Initialize(UsageStatsManagerWrapper.java:34)
at b4a.example.appscan.main._activity_create(main.java:342)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at b4a.example.appscan.main.afterFirstLayout(main.java:102)
at b4a.example.appscan.main.access$000(main.java:17)
at b4a.example.appscan.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)

I have to admit that my Asus tablet doesn't have a security, allow apps function within.
Any ideas on what other function might do this?
Rusty
 

MarcoRome

Expert
Licensed User
Longtime User
Hi Don,
I have tried to use your wrap and it gives me:


I have to admit that my Asus tablet doesn't have a security, allow apps function within.
Any ideas on what other function might do this?
Rusty
This will work Android 5.0 or >
 

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Marco, I'm using 6.5 that came out today :)
Any other ideas?
Rusty
 

Rusty

Well-Known Member
Licensed User
Longtime User
:oops: oops. Being illiterate is really tough ;)
Thanks. I'll try it on a "newer" device
Rusty
 

MarcoRome

Expert
Licensed User
Longtime User
If you see in your log you have this message:

java.lang.NoClassDefFoundError: android.app.usage.UsageStatsManager

upload_2016-12-12_22-19-22.png


So i think that you utilize device with Android Version < 5.0

upload_2016-12-12_22-21-45.png
 

Rusty

Well-Known Member
Licensed User
Longtime User
You are right, it is. It is a 4.4.2
I'm trying it right now on 6.0 (Marshmallow).
Thanks!
 

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Don,
I installed it on a Marshmallow tablet (api 23) and it works great :)
Good work!
Rusty
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
Hello,

I'm using it for blocking access to the standard webbrowser on devices with Android 5, Android 6 and Android 7 and it works nice.

One question remains: one needs to Add you App to the Exceptionlist in Android Security settings, Settings/Security/Allow app use of Usage stats

is it possible to read the settings and, if needed, go to the settings screen?
 

MarcoRome

Expert
Licensed User
Longtime User
Hello,

I'm using it for blocking access to the standard webbrowser on devices with Android 5, Android 6 and Android 7 and it works nice.

One question remains: one needs to Add you App to the Exceptionlist in Android Security settings, Settings/Security/Allow app use of Usage stats

is it possible to read the settings and, if needed, go to the settings screen?

You can use this code:
B4X:
Dim usage1 As List = usm.queryUsageStats(starttime,endtime)
    Log("Return if list is populate: " & usage1.Size)
    Dim check As Int = usage1.Size
    If check = 0 Then
        Msgbox("In Android 5.0 or above, permission must be granted for Lock to work correctly. Scroll to find ProtectYourApp and turn it ON","Look requires permission")
        ToastMessageShow("In Android 5.0 or above, permission must be granted for Lock to work correctly. Scroll to find ProtectYourApp and turn it ON", True)
        Dim StartIntent As Intent
        StartIntent.Initialize("android.settings.USAGE_ACCESS_SETTINGS", "")
        StartActivity(StartIntent)
        'Per Notifiche
        Dim In As Intent
        In.Initialize("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS", "")
        StartActivity(In)
    End If
 

frankly

Member
Licensed User
can anybody help me?




.Net version: 4.0.30319.42000
Ini folder: C:\Users\casa\AppData\Roaming\Anywhere Software\Basic4android
UDP server: 192.168.15.10, broadcast: 192.168.15.255
Build modules tree: 0
Gui: 1001
Gui: 1000
File not found: Starter.bas
Build modules tree: 1
File not found: Starter.bas
File not found: Starter.bas
*** no reuse ***
*** no reuse ***
Gui: 345
Gui: 255
Gui: 95
Gui: 71
Gui: 317
Gui: 74
Gui: 117
Gui: 144
Build modules tree: 4
Build modules tree: 0
*** no reuse ***
*** no reuse ***
Build modules tree: 2
Gui: 346
Compilation task: 406.3079
ExitCode: 1
Output:
jarsigner: unable to open jar file: bin\temp.ap_

Error:

jarsigner: unable to open jar file: bin\temp.ap_

ManagerSpecialLibraries: building jar - H:\ANDROID PROJETO\USMEx\UsageStatsManager\Objects\bin\extra\opt.jar
System.Exception: Maven artifact not found: android.arch.lifecycle/livedata-core
em fd.d(ad A_0, ICollection`1 A_1)
em g5.a(bj A_0, Dictionary`2 A_1, ICollection`1 A_2)
em g5.a(bj A_0, Dictionary`2 A_1, ICollection`1 A_2)
em g5.a(bj A_0, Dictionary`2 A_1, ICollection`1 A_2)
em g5.a(bj A_0, fv A_1, Boolean A_2, Boolean A_3)
em af.f()
em dp.a(Object A_0)
Gui: 116
Gui: 94
 

DonManfred

Expert
Licensed User
Longtime User
Maven artifact not found: android.arch.lifecycle/livedata-core
open B4A SDKManager and install them enter "livedata" in the searchfiel and install all found items.

For any other issue create a new thread in the Questionsforum.
 
Last edited:

Jehoschua

Member
Licensed User
Good evening

Thank you very much for this useful Library!

I have a probably simple to fix issue:
while working with this Library, it is very verbose because it creates many log-entries if queryAndAggregateUsageStats or queryUsageStats is called, e.g:

B4X:
…
Usage stats for: org.omnirom.omnijaws
Usage stats for: com.skype.raider
Usage stats for: com.google.android.ext.services
Usage stats for: com.android.providers.telephony
Usage stats for: com.android.providers.calendar
Usage stats for: com.android.providers.media
…

This makes it hard to debug the application which uses this Library.

Could you eventually provide us with the code so that we can remove the log() calls ourselves (and of course publish it again),
or could you please publish a version that does not create any Log() entries itself?


Thanks a lot for your support,
kind regards,
Thomas
 

DonManfred

Expert
Licensed User
Longtime User
Could you eventually provide us with the code so that we can remove the log() calls ourselves (and of course publish it again),
or could you please publish a version that does not create any Log() entries itself?
Sorry, but unfortunatly i lost the source last year due to a HDD Crash.
 

Jehoschua

Member
Licensed User
Sorry, but unfortunatly i lost the source last year due to a HDD Crash.

Thank you very much for your answer to this old subject / new question!,
I'm very sorry that you had suffered this damage.

If I find some time, I'll try to decompile it and test how it looks like. Maybe the effort is bearable :)

Kind regards, Thomas
 
Top