【问题标题】:Android time is reset after reboot重启后Android时间重置
【发布时间】:2018-04-04 08:52:31
【问题描述】:

我在某些设备中使用System.currentTimeMillis() 有问题(尤其是API 25、26 的模拟器,我认为还会有其他一些真实设备)。我的情况是:

1.在更改时间之前,我关闭了两个设置选项自动日期和时间自动时区,我从10h20 AM更改为11h20 AM =>时间已成功更改。和System.currentTimeMillis() 给出正确的更改时间11h20m AM

  1. 我重新启动设备,时间自动重置为10h20m AM,并且System.currentTimeMillis() 给我10h20m AM,而不是步骤1 中更改的时间。

那么,有什么想法可以检测安卓设备在重启后是否有“重置时间”问题?

【问题讨论】:

  • 我认为会有一些其他的真实设备 - 为什么
  • @TimCastelijns 目前,100% 使用模拟器 25、26 复制。但是,很少有用户对我的相关时间逻辑有问题。所以,我只是猜到了。

标签: android datetime


【解决方案1】:

在启动完成后创建一个 BroadcastReceiver 并使用它

创建一个意图过滤器:

     static {
        s_intentFilter = new IntentFilter();
        s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
        s_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
        s_intentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
    }

还有一个广播接收器:

    private final BroadcastReceiver m_timeChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(Intent.ACTION_TIME_CHANGED) ||
                action.equals(Intent.ACTION_TIMEZONE_CHANGED))
            {
                doWorkSon();
            }
        }
    };

注册接收者:

     public void onCreate() {
        super.onCreate();
        registerReceiver(m_timeChangedReceiver, s_intentFilter);     
    }

并取消注册:

     public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(m_timeChangedReceiver);     
    }

【讨论】:

  • @ahmad nemati 我现在你说,但主要问题是重启前更改的时间在重启后重置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
相关资源
最近更新 更多