Android Question Programmatically pin app to screen

knutf

Member
Licensed User
Longtime User
Hello

Can anyone help me with using StartLockTask() to pin app to screen?

My approach is as follows, but it does not have any effect on pinning the app:
B4X:
Sub Activity_Resume
    CallSubDelayed(Me,"KioskMode")
End Sub

Sub KioskMode()
    Dim jo As JavaObject
    jo.InitializeNewInstance("android.app.Activity",Null)
    jo.RunMethod("startLockTask",Null)
End Sub

(I does not know if it makes sense, but I use CallSubDelayed because the android documentation states that "..this method can only be called when the activity is foreground. That is, between onResume() and onPause()." )
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

knutf

Member
Licensed User
Longtime User
Are you using Device Owner / TaskLock / Kiosk apps 2017?
If not then probably you do not have the rights to pin a application.

So it is not possible to programmatically pin an app on an android device whitout first connecting the device to a PC and do some magic with the device first?

When I read the android documentation I got the impression that if no preperations is done on the android device "the current task will be launched into screen pinning mode. In this case, the system will prompt the user with a dialog requesting permission to use this mode. The user can exit at any time through instructions shown on the request dialog."
 
Upvote 0

knutf

Member
Licensed User
Longtime User
Try this:
Thank you, of course it was the InitializeContext methode that should be used!

There is still some problems that must be solved. Every time the user change the orientation, the Activity_Resume is called, this couses the
startLockTask methode to be called again. The user will then have the option to cancel the screen pinning mode without entering any password or code.
 
Upvote 0

knutf

Member
Licensed User
Longtime User
This was the solution:

B4X:
Sub Activity_Resume
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim activityManager As JavaObject = ctxt.RunMethod("getSystemService", Array("activity"))
    Dim LOCK_TASK_MODE_PINNED As Int = 2
    If activityManager.RunMethod("getLockTaskModeState", Null) <> LOCK_TASK_MODE_PINNED Then
        Dim joActivity As JavaObject
        joActivity.InitializeContext
        joActivity.RunMethod("startLockTask",Null)
    End If
End Sub
 
Upvote 0
Top