如何在android中设置移动系统时间和日期? [英] How to set mobile system time and date in android?

查看:52
本文介绍了如何在android中设置移动系统时间和日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您想在应用程序中更改移动系统的日期或时间时,您会怎么做?

When you want to change the mobile system date or time in your application, how do you go about doing it?

推荐答案

你不能在普通的现成手机上,因为它不可能获得 SET_TIME 权限.此权限具有 signatureOrSystem 的 protectionLevel,因此市场应用程序无法更改全球系统时间(但也许使用我还不知道的黑巫术魔法).

You cannot on a normal off the shelf handset, because it's not possible to gain the SET_TIME permission. This permission has the protectionLevel of signatureOrSystem, so there's no way for a market app to change global system time (but perhaps with black vodoo magic I do not know yet).

您不能使用其他方法,因为这在 Linux 级别上是被阻止的,(请参阅下面的长答案) - 这就是所有使用终端和 SysExecs 的试验都会失败的原因.

You cannot use other approaches because this is prevented on a Linux level, (see the long answer below) - this is why all trials using terminals and SysExecs gonna fail.

如果您获得许可是因为您将手机植根或构建并签署了自己的平台映像,请继续阅读.

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

这是可能的,并且已经完成.您需要 android.permission.SET_TIME.之后通过 AlarmManager 使用 AlarmManager>Context.getSystemService(Context.ALARM_SERVICE) 及其方法setTime().

It's possible and has been done. You need android.permission.SET_TIME. Afterward use the AlarmManager via Context.getSystemService(Context.ALARM_SERVICE) and its method setTime().

从 Activity 或 Service 将时间设置为 2010/1/1 12:00:00 的代码段:

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());

如果您要更改时区,方法应该非常相似(参见 android.permission.SET_TIME_ZONEsetTimeZone)

If you which to change the timezone, the approach should be very similar (see android.permission.SET_TIME_ZONE and setTimeZone)

正如在几个线程中指出的那样,只有 system 用户可以更改系统时间.这只是故事的一半.SystemClock.setCurrentTimeMillis() 直接写入 /dev/alarmsystem 拥有的设备文件,缺乏世界可写权限.所以换句话说,只有作为 system 运行的进程可以使用 SystemClock 方法.对于这种方式,android 权限无关紧要,不涉及检查适当权限的实体.

As it has been pointed out in several threads, only the system user can change the system time. This is only half of the story. SystemClock.setCurrentTimeMillis() directly writes to /dev/alarm which is a device file owned by system lacking world writeable rights. So in other words only processes running as system may use the SystemClock approach. For this way android permissions do not matter, there's no entity involved which checks proper permissions.

这是内部预装设置应用程序的工作方式.它只是在 system 用户帐户下运行.

This is the way the internal preinstalled Settings App works. It just runs under the system user account.

对于镇上的所有其他孩子来说,还有警报管理器.它是一个运行在 system_server 进程中的系统服务 - 你猜怎么着 - system 用户帐户.它公开了提到的 setTime 方法,但强制执行 SET_TIME 权限,然后只在内部调用 SystemClock.setCurrentTimeMillis (由于用户警报管理器正在运行).

For all the other kids in town there's the alarm manager. It's a system service running in the system_server process under the - guess what - system user account. It exposes the mentioned setTime method but enforces the SET_TIME permission and in in turn just calls SystemClock.setCurrentTimeMillis internally (which succeeds because of the user the alarm manager is running as).

干杯

这篇关于如何在android中设置移动系统时间和日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆