Android实践 -- 设置系统日期时间和时区

设置系统日期时间和时区

设置系统的日期时间和时区,需要 系统权限和系统签名android:sharedUserId="android.uid.system"
需要在manifest文件中添加相应的权限

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
  • 判断系统使用的是24小时制还是12小时制

    boolean is24Hour =  DateFormat.is24HourFormat(mContext);
    
  • 设置系统的小时制

24小时制

android.provider.Settings.System.putString(mContext.getContentResolver(),
              android.provider.Settings.System.TIME_12_24, "24");

12小时制

android.provider.Settings.System.putString(mContext.getContentResolver(),
              android.provider.Settings.System.TIME_12_24, "12");
  • 判断系统的时区是否是自动获取的

    public boolean isTimeZoneAuto(){
      try {
          return  android.provider.Settings.Global.getInt(mContext.getContentResolver(),
                  android.provider.Settings.Global.AUTO_TIME_ZONE) > 0;
      } catch (SettingNotFoundException e) {
          e.printStackTrace();
          return false;
      }
    }
    
  • 设置系统的时区是否自动获取

    public void setAutoTimeZone(int checked){
      android.provider.Settings.Global.putInt(mContext.getContentResolver(),
              android.provider.Settings.Global.AUTO_TIME_ZONE, checked);
    }
    
  • 判断系统的时间是否自动获取的

    public boolean isDateTimeAuto(){
      try {
          return android.provider.Settings.Global.getInt(mContext.getContentResolver(),
                  android.provider.Settings.Global.AUTO_TIME) > 0;
      } catch (SettingNotFoundException e) {
          e.printStackTrace();
          return false;
      }
    }
    
  • 设置系统的时间是否需要自动获取

    public void setAutoDateTime(int checked){
      android.provider.Settings.Global.putInt(mContext.getContentResolver(),
              android.provider.Settings.Global.AUTO_TIME, checked);
    }
    
  • 设置系统日期

    参考系统Settings中的源码

    public void setSysDate(int year,int month,int day){
      Calendar c = Calendar.getInstance();
      c.set(Calendar.YEAR, year);
      c.set(Calendar.MONTH, month);
      c.set(Calendar.DAY_OF_MONTH, day);
    
      long when = c.getTimeInMillis();
    
      if(when / 1000 < Integer.MAX_VALUE){
          ((AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when);
      }
    }
    
  • 设置系统时间

    参考系统Settings中的源码

    public void setSysTime(int hour,int minute){
      Calendar c = Calendar.getInstance();
      c.set(Calendar.HOUR_OF_DAY, hour);
      c.set(Calendar.MINUTE, minute);
      c.set(Calendar.SECOND, 0);
      c.set(Calendar.MILLISECOND, 0);
    
      long when = c.getTimeInMillis();
    
      if(when / 1000 < Integer.MAX_VALUE){
          ((AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when);
      }
    }
    
  • 设置系统时区

    public void setTimeZone(String timeZone){
      final Calendar now = Calendar.getInstance();
      TimeZone tz = TimeZone.getTimeZone(timeZone);
      now.setTimeZone(tz);
    }
    
  • 获取系统当前的时区

    public String getDefaultTimeZone(){
      return TimeZone.getDefault().getDisplayName();
    }
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,678评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,585评论 2 45
  • 今天在准备6月2号的公司分享,我没有过这方面的经验,也知道自己可能会紧张,但是我一点不害怕,因为我有个强力的武器—...
    迷夏的小岛阅读 248评论 0 3
  • 一、鹏 他无力的瘫坐在浴缸里,温热沿着腹部的创口,将清水染得越来越红. “我怎么能戒了你” 我和他自...
    草莓和猫阅读 345评论 0 0
  • 前两天接的两个顾客,把我格局一下放大了,一个是和我妈妈差不多大,属鸡,去小城故事吃饭,虽然来店里没买衣服,她给讲到...
    张严阅读 306评论 0 0