Android设置系统时间
16lz
2021-01-23
AlarmManager方式
/***************************************************************************** Prototype : getAndroidSysteTime Description : get android SystemTime Input : None Output : None Return Value : public Calls : Called By : History : 1.Date : 2018/9/28 Author : Modification : Created function *****************************************************************************/ public void getAndroidSysteTime(){ try { int zone = (TimeZone.getDefault().getRawOffset()) / 3600000; int mode = getHourMode(); if (mode != McuTimeInfo.ClockMode.INVALID.mode) { int year = Calendar.getInstance().get(Calendar.YEAR); int month = Calendar.getInstance().get(Calendar.MONTH) + 1; int day = Calendar.getInstance().get(Calendar.DATE); int hour = Calendar.getInstance().get(Calendar.HOUR); int minute = Calendar.getInstance().get(Calendar.MINUTE); int second = Calendar.getInstance().get(Calendar.SECOND); // 24小时制 if (McuTimeInfo.ClockMode.ROUND_HOUR.mode == mode) { hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); } Log.d(TAG, "get android system time,zone:" + zone + ",mode: " + mode + "," + year + "/" + month + "/" + day + "," + hour + ":" + minute + ":"+ second); } } catch (Exception e) { e.printStackTrace(); } } /***************************************************************************** Prototype : setAndroidSystemTime Description : set android system time Input : int mode int year int month int day int hour int minute int second Output : None Return Value : public Calls : Called By : History : 1.Date : 2018/9/28 Author : Modification : Created function *****************************************************************************/ public void setAndroidSystemTime(int mode, int year, int month, int day, int hour, int minute, int second){ Calendar c = Calendar.getInstance(); if (mode == Calendar.AM) { c.set(Calendar.AM_PM, Calendar.AM); } else { c.set(Calendar.AM_PM, Calendar.PM); } c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, day); c.set(Calendar.HOUR, hour);//HOUR_OF_DAY c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, second); c.set(Calendar.MILLISECOND, 0); long when = c.getTimeInMillis(); if (when / 1000 < Integer.MAX_VALUE) { ((AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when); } }
###需要配置权限,并且app要具有系统权限
Android 网路校时 NetworkTimeUpdateService
time.android.com 86400000 60000 3 5000 5000
更多相关文章
- Android 应用获取通知栏权限
- Android的权限机制之—— “沙箱”机制sharedUserId跟签名
- Android Manifest.permission权限
- Android 系统下模拟触点击的功能与权限
- android 笔记 --- Android各种访问权限Permission详解
- Android 操作权限大全(已归纳分类)
- Android root权限获取大揭秘
- Android开发录音和播放音频的步骤(动态获取权限)
- Android权限【大全】