Programmatically set System Time
As it turns out, the answer did lie in the referenced question found here. However, the command is different for Android 7 (and possibly 6, I have not tested) devices. I do not have the reputation to comment over there, so if someone wishes to paste/reference this answer on that question, go right ahead.
The date code I used was in the format MMddhhmmyy, rather than yyyyMMdd.hhmmss; this works for me on Android 7. I also removed the appended '-s'. The full working code I used to set the system time is below. Again it should be noted that this requires rooting the device so is only useful in certain scenarios like mine.
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
String command = "date 1123104017\n";
// Log.e("command",command);
os.writeBytes(command);
os.flush();
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (InterruptedException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
The date I entered, for clarity, is November 23rd 2017 at 10:40 AM.
Set Android's date/time programmatically
The user application does not have permission to change the device time. Please read the answer by cashbash in the following post for the alternate option.
Copying here for quick reference:
According to this thread, user apps cannot set the time, regardless of the permissions we give it. Instead, the best approach is to make the user set the time manually. We will use:
startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));
Unfortunately, there is no way to link them directly to the time setting (which would save them one more click). By making use of ellapsedRealtime
, we can ensure that the user sets the time correctly.
Can I programmatically change time on device?
Edit: Try
AlarmManager a = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
a.setTime(millis);
Don't forget permission SET_TIME
in your manifest.
Related Topics
When to Register/Unregister Broadcast Receivers Created in an Activity
Lazy Download Images into Gridview
Startactivity() from Broadcastreceiver
Android Facebook 4.0 Sdk How to Get Email, Date of Birth and Gender of User
Using Resultreceiver in Android
Android Webview "Tel:" Links Show Web Page Not Found
Popupwindow - Dismiss When Clicked Outside
Neither User 10102 Nor Current Process Has Android.Permission.Read_Phone_State
How to Automatically Add Thousand Separators as Number Is Input in Edittext
Android M Camera Intent + Permission Bug
How to Crop an Image in Android
Simulate Low Network Connectivity for Android
Read & Writing Arrays of Parcelable Objects
Android: Resize Only Parts of View with Soft Keyboard on Screen