Setting System Time of Rooted Phone

How to set time zone for android rooted device?

If you have the correct permission(see below), you can do this with the AlarmManager. For example, to set the time to 2013/08/15 12:34:56, you could do:

Calendar c = Calendar.getInstance();
c.set(2013, 8, 15, 12, 34, 56);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.setTime(c.getTimeInMillis());

You need the permission SET_TIME to do this. Unfortunately, this is a signatureOrSystem permission.

Definition in AndroidManifest.xml:

<!-- Allows applications to set the system time -->
<permission android:name="android.permission.SET_TIME"
android:protectionLevel="signature|system"
android:label="@string/permlab_setTime"
android:description="@string/permdesc_setTime" />

The only apps that can use this permission are:

  • Signed with the system image
  • Installed to the /system/ folder

Unless you build custom ROMs, you're out of luck with the first.

For the second, it depends on what you are doing.

  • If you're building an app for wide distribution(Play Store, etc), you
    probably shouldn't. It's only an option for root users, and can only
    be installed manually. Any marketplace would not install it to the
    correct location.
  • If you're building an app for yourself(or just as a learning
    exercise), go for it. You'll need a rooted phone, though, so do that
    first. You can then install the application straight to /system/app/
    with adb or a file manager. See articles like this for more detail.

One final note: The SET_TIME permission and AlarmManager#setTime() were added in Android 2.2(API 8). If you're trying to do this on a previous version, I'm not sure it will work at all.

Android: How can I get the set_time permission?

Well I read up some more, and I'm still not sure if its possible to use an NTP server even when building my own ROM. Most android devices will use NITZ to get the date from the mobile network, but my target device is WIFI only.

I found the answer here Setting system time of ROOTED phone - specifically @CrazyCoder's answer that revealed how his ClockSync app works.

This solution requires the device to be rooted, but does not need to be a system app or any of the permissions that go with that.



Related Topics



Leave a reply



Submit