如何以编程方式设置设备的时间 [英] How to set time to device programmatically

查看:239
本文介绍了如何以编程方式设置设备的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个应用程序来替换当前显示时间与服务器时间。我已经尝试了很多,但是我没有找到解决方案。我如何将其设置为我的设备?

I have created one application that replace the current showing time with server time. I have tried a lot, but I didn't find a solution. How can I set it to my device?

我知道如何让我的Android设备上的服务器时间。是否可能实时,特别是Android?

I know how to get the server time on my Android device. Is it possible in real time, specially on Android?


  • 服务器时间会自动设置为我的按钮点击事件的设备。 >
  • Server Time is automatically set to my device on my button click event.

推荐答案

如果您有正确的权限(见下文),您可以使用 AlarmManager 执行此操作。例如,要将时间设定为2013/08/15 12:34:56,您可以执行以下操作:

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






您需要权限 SET_TIME 来做到这一点。不幸的是,这是一个 signatureOrSystem 权限。


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

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:


  • 使用系统映像签名

  • 安装到 / system / 文件夹

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

除非您构建自定义ROM,否则第一个没有运气。

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

第二个,这取决于你在做什么。

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


  • 如果您正在构建广泛发布的应用程序( Google Play 等),您可能不应该。这只是root用户的选择,您只能手动安装。任何市场都不会将其安装到正确的位置。

  • If you're building an app for wide distribution (Google Play, etc.), you probably shouldn't. It's only an option for root users, and you'll only be able to install it manually. Any marketplace would not install it to the correct location.

如果您为自己构建应用程序(或作为学习练习),请继续。但是,您需要一个根深蒂固的手机。然后,您可以使用 / system / app / > ADB 或文件管理器。有关更多信息,请参阅这样的文章细节。

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.

最后一个注释: SET_TIME 权限和 AlarmManager#setTime()已添加到Android 2.2( API 8 )。如果您尝试在以前的版本中执行此操作,我不确定它是否正常工作。

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.

这篇关于如何以编程方式设置设备的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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