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);

}

//*/

}

}

相关推荐
荣月灵的小梅花2 小时前
Android 给广播接收器增加权限(permission)或signature签名权限
android
沐言人生3 小时前
ReactNative 源码分析4——ReactActivity之加载JSBundle
android·react native
砖厂小工4 小时前
Now In Android 精讲 10 - AGENTS.md:写给 AI Agent 的项目说明书
android
Ehtan_Zheng5 小时前
Jetpack Compose 动画转换编排的艺术
android
Ehtan_Zheng6 小时前
Jetpack Compose 动画入门:轻松掌握状态驱动的动画转换
android
Ehtan_Zheng6 小时前
Jetpack Compose 布局与可见性动画
android
_李小白6 小时前
【android opencv学习笔记】Day 12: HSV 色彩空间
android·opencv·学习
千里马学框架6 小时前
手机大厂Activity嵌套模式及三分屏SplitScreen功能调研报告-独家干货
android·智能手机·分屏·aaos·安卓framework开发·车机·三分屏
Mr.QingBin6 小时前
SystemUI插件开发指南
android
芋只因7 小时前
MySQL 分库分表与 MyCat 的使用
android