亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Android實踐 -- 設置系統日期時間和時區

標簽:
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();
    }



作者:CodeMiner
链接:https://www.jianshu.com/p/6c6a6091545d

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消