编程方式获得网络运营商的时间(机器人) [英] Programmatically get the network operator's time (android)

查看:339
本文介绍了编程方式获得网络运营商的时间(机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获得当前的时间为我的Andr​​oid应用程序。时间应操作者的时间,而不是在本地时间由用户设定。

I need to get the current time for my android app. The time should be operator's time and not the local time set by the user.

TelephonyManager telephonyManager =((TelephonyManager)Context.getSystemService(Context.TELEPHONY_SERVICE));
String operatorName = telephonyManager.getNetworkOperatorName();

这是编程获取运营商的名称。可是如何才能让运营商当前的时间?如果用户手动更改了时间,那么应用程序将其更改为运营商的当前时间。

This is to get the operator's name programmatically. But how to get the operator's current time? If the user has changed the time manually, then the app will change it to operator's current time.

我做了一个<一个href=\"https://www.google.co.in/search?q=get%20network%20operator%20time%20android&ie=utf-8&oe=utf-8&rls=org.mozilla%3aen-US%3aofficial&client=firefox-a&gws_rd=cr#bav=on.2,or.r_cp.r_qf.&ei=0AESUompHIONrQfv0IHoDQ&fp=1d6e54d72f553b01&q=network%20operator%20time%20retrieve%20android&rls=org.mozilla%3aen-US%3aofficial&sa=X&spell=1&ved=0CCkQvwUoAA\"相对=nofollow>谷歌搜索太:

无法找到一个更好的。如果已经问计算器上,借口请提供链接。

Unable to find a better one. If its already asked on stackoverflow, excuse and please provide the link.

编辑:

我忘了提,这是一个离线应用,在那里我没有得到来自用户的上网权限。

I forgot to mention that it is an offline app where i am not getting the internet permission from the user.

感谢。
(抱歉坏英文)

Thanks. (sorry for bad English)

推荐答案

简单的答案是,它无法做到的。为了完成你想要什么,你的应用程序将需要做两件事情:

The simple answer is that it cannot be done. In order to accomplish what you want, your app would need to do two things:


  1. 发送服务消息给网络操作员;和

  2. 在设备上更改本地时间。

这两个操作都需要系统级权限。也就是说,你的应用程序需要使用系统密钥签名(即,一个由谷歌发出的设备制造商),以访问这些功能。

Both of these operations require system-level privileges. That is, your app needs to be signed with a system key (i.e. one issued by Google to device manufacturers) in order to access these functions.

您的能有什么的做的是从NTP服务器获取当前时间。有在互联网上大量免费使用的NTP服务器。样本code来获得这样的时间是这样的:

What you can do is get the current time from an NTP server. There are plenty free-to-use NTP servers on the internet. A sample code to get such time is this:

String timeServer = "server 0.pool.ntp.org";

NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(timeServer);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getReturnTime();

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(returnTime);

现在你的 CAL 对象包含的真正的从NTP服务器,而不是从设备的时间的时间。不过,你将不能够设定时间的设备的当前时间。

Now your cal object contains the real time from the NTP server and not the time from the device. Still, you will not be able to set the time as the device's current time.

这篇关于编程方式获得网络运营商的时间(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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