Android 重新定义一个广播修改系统时间,避免系统时间混乱

有时候,搞不懂为什么手机设备无法准确定义系统时间,出现混乱或显示与实际不符,需要重置或重新设定一次才行,也是真的够无语的!!

vendor/mediatek/proprietary/packages/apps/MtkSettings/AndroidManifest.xml

<!--无法从设置外部触发。统计数据可以触发它,因为我们发送

对它犹豫不决-->

<receiver android:name=".fuelgauge.batterytip.AnomalyConfigReceiver "

android:exported="true">

<intent-filter>

<action android:name="android.app.action.STATSD_STARTED"/>

<action android:name="android.intent.action.BOOT_COMPLETED"/>

<action android:name="com.android.set.system_time"/>

</intent-filter>

</receiver>

/vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java

//*/broadcast modify system time

import android.provider.Settings;

import android.app.AlarmManager;

//*/

public class AnomalyConfigReceiver extends BroadcastReceiver {

private static final String TAG = "AnomalyConfigReceiver";

@Override

public void onReceive(Context context, Intent intent) {

if (StatsManager.ACTION_STATSD_STARTED.equals(intent.getAction())

|| Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {

final StatsManager statsManager = context.getSystemService(StatsManager.class);

// Check whether to update the config

AnomalyConfigJobService.scheduleConfigUpdate(context);

try {

BatteryTipUtils.uploadAnomalyPendingIntent(context, statsManager);

} catch (StatsManager.StatsUnavailableException e) {

Log.w(TAG, "Failed to uploadAnomalyPendingIntent.", e);

}

if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {

AnomalyCleanupJobService.scheduleCleanUp(context);

}

}

//*/add broadcast modify system time

if ("com.android.set.system_time ".equals(intent.getAction())) {

String tzId = intent.getStringExtra("timeZone");
String timeFormat = intent.getStringExtra("timeFormat");
long when = intent.getLongExtra("when",0);

android.util.Log.i("TP","com.tyd.system_time tzId = "+tzId);

android.util.Log.i("TP","com.tyd.system_time timeFormat = "+timeFormat);

android.util.Log.i("TP","com.tyd.system_time when ="+when);

Settings.Global.putInt(context.getContentResolver(), Settings.Global.AUTO_TIME_ZONE,0);//【置0重新设置一次】
context.getSystemService(AlarmManager.class).setTimeZone(tzId);
android.provider.Settings.System.putString(context.getContentResolver(),
android.provider.Settings.System.TIME_12_24, timeFormat);
context.getSystemService(AlarmManager.class).setTime(when);

}

//*/

}

}

相关推荐
Android小码家1 小时前
Framework之Launcher小窗开发
android·framework·虚拟屏·小窗
赏金术士2 小时前
第七章:状态管理实战与架构总结
android·ui·kotlin·compose
颂love3 小时前
MySQL的执行流程
android·数据库·mysql
云起SAAS7 小时前
抖音小游戏源码 - 消消乐 | 含激励广告+成就系统 | 开箱即用商业级消除游戏模板
android·游戏·广告联盟·看激励广告联盟流量主·抖音小游戏源码 - 消消乐
大貔貅喝啤酒9 小时前
基于Windows下载安装Android Studio 3.3.2版本教程(2026详细图文版)
android·java·windows·android studio
程序员码歌9 小时前
OpenSpec 到 Superpowers:AI 编码从说清到做对
android·前端·人工智能
2501_915106329 小时前
深入解析无源码iOS加固原理与方案,保护应用安全
android·安全·ios·小程序·uni-app·cocoa·iphone
黄林晴12 小时前
重磅官宣:Android UI 开发正式进入 Compose-first 时代
android·google io
Kapaseker13 小时前
搞懂变换!精通 Compose 绘制(二)
android·kotlin
美狐美颜SDK开放平台13 小时前
美颜SDK开发详解:如何优化美颜SDK在低端安卓机上的性能?
android·ios·音视频·直播美颜sdk·视频美颜sdk