如何设置手机系统时间和日期的Andr​​oid?如何设置、日期、时间、系统

2023-09-11 12:01:48 作者:孤傲的背影

当你想改变移动系统日期或时间在你的应用程序,你怎么去这样做呢?

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

推荐答案

您不能在正常的现成手机,因为它无法获得SET_TIME权限。此权限有protectionLevel对 signatureOrSystem ,所以没有办法对市场的应用程序,以改变全球体系的时间(但也许用黑vodoo魔法,我不知道呢)。

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).

您无法使用其他方法,因为这是pvented在Linux级别$ P $,(请参阅下面的长的答案) - 这就是为什么使用终端和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通过 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 it s method setTime().

片段从一种活动或服务设置时间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_ZONE 和setTimeZone)

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

因为已经指出的几个线程,只有系统用户可以更改系统时间。这只是故事的一半。 SystemClock.setCurrentTimeMillis()直接写入的/ dev /报警这是系统缺乏世界可写权限。因此,换句话说只运行的进程系统可以使用 SystemClock 办法。对于这样的Andr​​oid权限并不重要,有没有涉及的实体,它检查适当的权限。

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.

这是内部preinstalled设置应用程序的工作方式。它只是运行在系统用户帐户。

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

对于所有在城里其他孩子有报警经理。这是在 system_server 程序下运行的系统服务 - 你猜怎么着 - 系统用户帐户。它公开提到的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).

~~~干杯