Android: Granting SET_TIME permission as a Device Owner using Device Policy Manager

1.9k views Asked by At

I am developing a COSU/KIOSK application and I need to manually update the time on the device.

I am using AlertManager.setTime(Calendar) to do so, but I can't grant my application the SET_TIME permission that it is required.

The application is the device owner, and this allowed me to use many other system permissions, for example

android:name="android.permission.REBOOT"
android:name="android.permission.SHUTDOWN"
android:name="android.permission.WRITE_SETTINGS"
android:name="android.permission.INSTALL_PACKAGES"
android:name="android.permission.DELETE_PACKAGES"

All of these permissions were granted to my application, just by listing them in the manifest.xml

But SET_TIME does not work.

I also tried using the device policy manager

mDevicePolicyManager.setPermissionGrantState(mAdminComponentName, getPreferredPackageName(),
                Manifest.permission.SET_TIME, DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);

This function returned false meaning it couldn't grant permission.

How can I solve this problem without prompting the user to grant this permission to my application.

1

There are 1 answers

2
Pavel Luzhetskiy On

If you are targeting devices api 28+ (android 9 and higher) you can set it with setTime(ComponentName admin, long millis) function.

For lower android versions I had to rely on a device manufacturer api to set device time.

Manifest.permission.SET_TIME is proteced/system permission, sadly deviceOwner cannot grant any protected permissions with setPermissionGrantState() function. There is very important word in its documentation:

Sets the grant state of a !runtime! permission for a specific application.