简体   繁体   中英

How to set time zone for android rooted device?

I have a rooted android device and I want to change my device's default time, date and time zone from my application. Changing time and date is quite easy, but how can I change my device's timezone?

You need android.permission.SET_TIME . Afterward use the AlarmManager via Context.getSystemService(Context.ALARM_SERVICE) and it s method setTime() .

Snippet for setting the time to 2010/1/1 12:00:00 from 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 which to change the timezone, the approach should be very similar (see android.permission.SET_TIME_ZONE and setTimeZone )

more info in this link and this

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM