Android Question [SOLVED] Check if usagestats are enabled for your app

Jmu5667

Well-Known Member
Licensed User
Hello

Is there a method to check if usage stats are enabled for your application, similar to draw over other app method shown below:

B4X:
Sub Class_Globals
   Private ion As Object
   Private phone As Phone
End Sub

Public Sub Initialize
End Sub

Public Sub GetPermission As ResumableSub
   If phone.SdkVersion >= 23 Then
       Dim settings As JavaObject
       settings.InitializeStatic("android.provider.Settings")
       Dim ctxt As JavaObject
       ctxt.InitializeContext
       If settings.RunMethod("canDrawOverlays", Array(ctxt)) = True Then
           Return True
       End If
       Dim i As Intent
       i.Initialize("android.settings.action.MANAGE_OVERLAY_PERMISSION", "package:" & Application.PackageName)
       StartActivityForResult(i)
       Wait For ion_Event (MethodName As String, Args() As Object)
       Return settings.RunMethod("canDrawOverlays", Array(ctxt))
   Else
       Return True
   End If
End Sub

Private Sub StartActivityForResult(i As Intent)
   Dim jo As JavaObject = GetBA
   ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
   jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Private Sub GetBA As Object
   Dim jo As JavaObject = Me
   Return jo.RunMethod("getBA", Null)
End Sub

Regards

John.
 

Erel

Administrator
Staff member
Licensed User
B4X:
Sub IsUsageStatsPermissionGranted As Boolean
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim AppOps As JavaObject = ctxt.RunMethod("getSystemService", Array("appops"))
   Dim Process As JavaObject
   Process.InitializeStatic("android.os.Process")
   Dim mode As Int = AppOps.RunMethod("checkOpNoThrow", Array("android:get_usage_stats", Process.RunMethod("myUid", Null), Application.PackageName))
   Return mode = 0 'AppOpsManager.MODE_ALLOWED
End Sub
Based on: https://stackoverflow.com/a/28921586/971547
 
Top