【问题标题】:set device time programmatically in android [duplicate]在android中以编程方式设置设备时间[重复]
【发布时间】:2014-12-07 15:33:20
【问题描述】:

我需要动态设置设备时间。如果可以的话请指导我

我试过了

MainActivity.java

Calendar c = Calendar.getInstance();    
c.set(2010, 1, 1, 12, 00, 00);

ma​​nifest.xml

<permission android:name="android.permission.SET_TIME"
    android:protectionLevel="signatureOrSystem"
    android:label="@string/app_name" />

提前致谢

【问题讨论】:

  • menifest.xml 在哪里?
  • 试试我的回答,投反对票的人也在投反对票之前给出描述
  • @Glenn-- 我已经在关注此代码,但我收到类似这样的错误“原因:java.lang.SecurityException:setTime:用户 10095 和当前进程都没有 android.permission.SET_TIME。”
  • @SpryTechies 我正在使用你的代码,我的日志猫出现上述错误
  • stackoverflow.com/questions/1332269/… 看看这个链接它会解决你的错误

标签: android datetime time timezone


【解决方案1】:
//set Time to device
    Calendar c = Calendar.getInstance();
    c.set(2013, 8, 15, 12, 34, 56);
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    am.setTime(c.getTimeInMillis());

//   and set in Manifest.xml
 <!-- Allows applications to set the system time -->
    <permission android:name="android.permission.SET_TIME"
        android:protectionLevel="signature|system"
      />

点击此链接获取更多帮助How to set mobile system time and date in android? 我觉得可能对你有帮助

【讨论】:

  • 请用一段代码添加一些描述,以便更好地理解。
  • 这给了我错误 java.lang.SecurityException: setTime: 用户 10298 和当前进程都没有 android.permission.SET_TIME。
猜你喜欢
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
相关资源
最近更新 更多