How to set mobile system time and date in android? –

January 14, 2021 by No Comments

Development issue/problem:

If you want to change the date or time on your application’s mobile system, how do you do it?

How can I solve this problem?

Solution 1:

It is not possible to get SET_TIME resolution on a normal phone. This authorization has a signatureOrSystem level of protection, so there is no way for the market application to change the global system time (but maybe with some black voodoo magic I don’t know about yet).

You can’t use other approaches, because this is prevented in Linux (see the long answer below) – so all tests with Terminals and SysExecs will fail.

If you CAN get permission because you either rooted your phone or built and signed your own platform image, read on.

Short answer

It is possible and it has already been done. You need android.SET_TIME. Then use the alarm handler via Context.getSystemService(Context.ALARM_SERVICE) and its method s setTime().

Fragment to set the time at 2010/1 12:00:00 of an activity or service :

Calendar c = Calendar.getInstance();
c.set(2010, 1, 1, 12, 00, 00);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.setTime(c.getTimeInMillis())

If you want to change the time zone, the procedure should be very similar (see android.permission.SET_TIME_ZONE and setTimeZone).

Long answer

As mentioned in several threads, only the user of the system can change the system time. That’s only part of the story. SystemClock.setCurrentTimeMillis() writes directly to /dev/alarm, which is a device file that belongs to a system with no global write permission. In other words: Only processes running on the system can use the SystemClock approach. For this method of consent, the android is irrelevant, there is no authority that checks for correct consents.

Here’s how the internal, pre-installed Settings application works. It will simply function as a system user account.

For every other kid in town, there’s a tipster. This is a system service that runs in the system_server process under the account – guess what – of the system user. It exposes the above setTime method, but applies the SET_TIME permission and in turn just calls SystemClock.setCurrentTimeMillis internally (which succeeds thanks to the user on whose behalf the alert handler is executed).

Cheers.

Solution 2:

According to this thread, custom applications cannot set the time no matter what permissions we give them. It is preferable to force the user to set the time manually. We use :

startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS)) ;

Unfortunately, there is no way to link it directly to the time settings (which would save it for another click). By using ellapsedRealtime, we can ensure that the user has set the time correctly.

Solution 3:

The solution for root devices may be to run commands.

  • su
  • Date -s YYYYMMDD.HHMMSS

This can be done with the following code:

private void changeSystemTime(String year,string month,string day,string hour,string minute,string second){try {Process process = Runtime.getRuntime().exec(su);DataOutputStream os = new DataOutputStream(process.getOutputStream());String command = date -s+year+month+day++hour+minute+second+n;Log e(command,command;os writeBytes(command).+h+min+sec+n;Log e(command,command);os.writeBytes(command);os.flush();process.waitFor();} catch (InterruptedException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}

Just call the previous method as follows:

changeSystemTime(2015,04,06,13,09,30);

Solution 4:

I haven’t seen it anywhere, but it works for me. My device is root and I have the Superuser installed, but if the Superuser is running on non-root devices, it may work. I used AsyncTask and called the following:

protected String doInBackground(String…params){
Runtime.getRuntime().exec(su && date -s + params[0]);}

Solution no. 5:

In our case, there is an accidental bypass:

If the user is connected to the Internet, we get the Internet time (NTP server) and compare the difference (-) of the internal device time (registerOffsetFromInternetTime). We store them in the user’s configuration log file.

We use devide + registeredOffsetFromInternetTime to take into account the correct update time of our application.

All GETHOUR processes check the difference between the current time and the last comparison time (with the internet time). If the time is greater than 10 minutes, run a new equation to update RegegederOffsetFromInternetTime to maintain accuracy.

If the user is using an application without internet, we can only store the registered application OffsetFromInternetTime and use it as a reference. If the user just changes the time on the local offline device and uses the application, it will read the wrong time. However, when the user comes back online, we warn them that the time has changed and ask them to resync downstream or upstream updates made offline with an incorrect time.

Solution no. 6:

Thank you, Penquin. In Quickshortcutmaker, I enter the name of the activity to set the date and time so I can start setting the system time:

Intent=new Intent() ;
int.setComponent(new ComponentName(com.android.settings,
com.android.settings.DateTimeSettingsSetupWizard)) ;
startActivity(intention) ;

`

Good luck!

Related Tags:

 how do i change the time on my phonephone date settingset current timedate and time settings androidset time and date on my phonehow to change the date on your phone androiddisplay time settingset the time on my phone pleasechange time zone androidhow to set time in micromax mobileandroid set timezone programmaticallywhy is the time wrong on my androidhow to set mobile system time and date in android programmaticallyandroid automatic date and time programmaticallyhow do i change the time on my samsung phone

Leave a Comment

Your email address will not be published. Required fields are marked *