Is there an api to set ntp time server for android?

44 Views Asked by At

I know that from ADB, to set the time server on Android for ntp you can use the command:

adb shell /system/bin/settings put global ntp_server SERVER

Where SERVER would be the new ntp time server. This works fine from the command line, but I am trying to do this with code. Below is my code for a Xamarin.Forms app. I do not believe the issue is that it is written in c# vs Java or Kotlin.

public void SetTimeServer(string timeServer)
{
    var process = new System.Diagnostics.Process();
    process.StartInfo.FileName = @"/system/bin/settings";
    process.StartInfo.Arguments = $"put global ntp_server {timeServer}";
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardInput = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.OutputDataReceived += (sender, e) => { System.Diagnostics.Debug.WriteLine(e.Data); };
    process.ErrorDataReceived += (sender, e) => { System.Diagnostics.Debug.WriteLine(e.Data); };
    process.Start();
    process.BeginOutputReadLine();
    process.BeginErrorReadLine();
    process.WaitForExit();
}

When I run this code, I see the following error:

[0:] Security exception: Permission Denial: getCurrentUser() from pid=6567, uid=10100 requires android.permission.INTERACT_ACROSS_USERS
[0:] 
[0:] 
[0:] java.lang.SecurityException: Permission Denial: getCurrentUser() from pid=6567, uid=10100 requires android.permission.INTERACT_ACROSS_USERS
[0:]    at com.android.server.am.UserController.getCurrentUser(UserController.java:1878)
[0:]    at com.android.server.am.ActivityManagerService.getCurrentUser(ActivityManagerService.java:17631)
[0:]    at com.android.providers.settings.SettingsService$MyShellCommand.onCommand(SettingsService.java:259)
[0:]    at android.os.ShellCommand.exec(ShellCommand.java:104)
[0:]    at com.android.providers.settings.SettingsService.onShellCommand(SettingsService.java:49)
[0:]    at android.os.Binder.shellCommand(Binder.java:881)
[0:]    at android.os.Binder.onTransact(Binder.java:765)
[0:]    at android.os.Binder.execTransactInternal(Binder.java:1021)
[0:]    at android.os.Binder.execTransact(Binder.java:994)

Changing the code to use the Java equivalent does line:

        Java.Lang.Runtime.GetRuntime().Exec($"settings put global ntp_server {timeServer}");

does not appear to make any difference. It still doesn't work.

Updating AndroidManifest.xml with

<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS"/>

also does not solve the problem.

Has anyone run in to this before? How would I solve the problem?

0

There are 0 best solutions below