I am new to Android in Xojo. I am trying to develop an Android App that needs to obtain the usage statistics to do some things.
I add a declare in a Method of the App, like this
Declare Function stat1 Lib "android.app.usage.UsageStatsManager" Alias "queryUsageStats" (intervalType As Integer, beginTime as Integer, endTime as Integer) as Integer
mytotalTime = stat1(0,startTime, endTime)
The Android compiler returns and error: “Unresolved reference to queryUsageStats”.
The permission is granted in the Build section of xojo Android to use stats like this:
android.permission.PACKAGE_USAGE_STATS
Declare Function stat1 Lib "android.app.usage.UsageStatsManager" Alias "queryUsageStats" (intervalType As int32, beginTime as int64, endTime as int64) as Integer
mytotalTime = stat1(0,startTime, endTime)
Thanks for the quick turnaround. I changed the types as suggested but got the same error. I also tried with the type of the function (currently integer) changed to ptr, but does not work either.
I saw somewhere that there may be a need to aslo declare a “context” but did not try yet.
I did not do any Android declares, so I might be wrong, but if I understand the docs correctly QueryUsageStats is a method of UsageStatsManager which is a class of the usage framework. So if Android is somewhat similar to other OS’, the function should be declared on this object, not on the library itself.
And it will return a Ptr, not an Integer.
Hi Ulrich, could you explain what you mean by the ‘function should be declared on this object not on the library’?
I have modified my code to try declare the function as an object via its constructor, and then call the function of its instance like this (starTime and endTime are obtained earlier in the code):
dim totalTime as ptr
Declare Function UsageStatsManager Lib "android.app.usage.UsageStatsManager" () As Ptr
Var mastat As Ptr = UsageStatsManager
Declare Function queryUsageStats Lib "android.app.usage.UsageStatsManager.instance" (ref As Ptr, intervalType As int32, beginTime as int64, endTime as int64) As ptr
totalTime = queryUsageStats(mastat,0,startTime, endTime)
but the android compiler returns an error because the constructor of the UsageStatsManager object is private:
Cannot access ‘constructor(): UsageStatsManager’: it is package-private in ‘android/app/usage/UsageStatsManager’.
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ‘:app:compileDebugKotlin’.
A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
Compilation error. See log for more details
Try:
Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
Get more help at https://help.gradle.org.
In parallel I tried to create my own Android aar library to simply return totaltime, to further use it in Xojo via declare, but it will not compile in Android Studio because the Kotlin metadata version needed to interact with Xojo is 1.6.0 while my android studio set up obtains 1.8.0. and I did not figure out how to change this.
As I said, I did never do any Android declares, so I hope somebody with more experience can chime in.
But from what I found, you get the StatsManager from core.context.GetSystemService. As this is a system service, it makes sense you cannot instantiate another one.