【问题标题】:Broadcast Receiver for DATE_CHANGED not Working Only for TIMEZONE_CHANGEDDATE_CHANGED 的广播接收器仅适用于 TIMEZONE_CHANGED
【发布时间】:2019-03-05 21:57:57
【问题描述】:

我无法让我的 BroadcastReciever() 为 DATE_CHANGED 工作,但在 TIMEZONE_CHANGED 上工作得很好。

class DateChangeReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        Log.d("my broadcast","works")
        if (intent.action == Intent.ACTION_DATE_CHANGED ||
                intent.action == Intent.ACTION_TIMEZONE_CHANGED) {
            var netUtils = NetUtils(context)
            netUtils.mainStack()
        }
    }
}

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SET_WALLPAPER" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/castle"
        android:roundIcon="@drawable/castle"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.VIEW" />

            </intent-filter>
        </activity>
        <receiver android:name=".DateChangeReceiver">
            <intent-filter>
                <action android:name="android.intent.action.DATE_CHANGED"/>
                <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

这是我在主要活动中的功能:

 fun keepAlive() {

    //KEEP RUNNING IN BACKGROUND TO UPDATE WALLPAPER AT CHANGE OF DAY
     val component = ComponentName(this@MainActivity, DateChangeReceiver::class.java)
    packageManager.setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP)
     Log.d("KEEPALIVE FUN","works")

 }

keepAlive()

当我手动更改日期或将时间设置为 11:59 并等待一分钟时,我在 logcat 中什么也得不到。

在我添加接收器之前,我在 logcat 中确实出现了几个错误:

2019-03-05 11:48:01.884 29176-29202/com.test E/libc: Access denied finding property "vendor.debug.egl.profiler"
2019-03-05 11:48:07.616 29176-29234/com.test E/libc: Access denied finding property "ro.vendor.graphics.memory"
2019-03-05 11:48:07.665 29176-29234/com.test E/libc: Access denied finding property "vendor.gralloc.enable_ahardware_buffer"

这些是否可能会影响我的 BroadcastReciever?到目前为止,我已经在 3 台设备上对此进行了测试。

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    通过切换到 AlarmManager 解决了这个问题。如果有人好奇,这是代码:

    val calendar: Calendar = Calendar.getInstance().apply {
        timeInMillis = System.currentTimeMillis()
        set(Calendar.HOUR_OF_DAY, 3)
    }
    

    这种方法对我来说效果更好,因为它可以让所有设备与我的应用同时更新,以适应新的一天的开始。

    val alarmMgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val broadcastIntent = Intent(this, DateChangeReceiver::class.java)
    val pIntent = PendingIntent.getBroadcast(this,0,broadcastIntent,0)
    
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.timeInMillis,
            AlarmManager.INTERVAL_DAY,
            pIntent
    

    【讨论】:

      【解决方案2】:

      我为更改日期隐式注册了广播接收器。这对我有用。

      registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_DATE_CHANGED));
      

      【讨论】:

        【解决方案3】:

        根据这个question,可能有一个bug。它现在已经过时了,但你可以尝试提交新的问题。

        这个question 也可以帮助你。

        他们使用

        ACTION_TIME_TICK

        ACTION_TIMEZONE_CHANGED

        ACTION_TIME_CHANGED

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

        【讨论】:

        • TICK 每分钟执行一次,TIME_CHANGED 用于手动更改时间,对吗?有人 here 建议 DATE 已贬值。
        猜你喜欢
        • 2022-06-11
        • 1970-01-01
        • 2022-05-14
        • 1970-01-01
        • 1970-01-01
        • 2017-07-10
        • 2019-03-02
        • 2013-09-17
        • 2012-12-18
        相关资源
        最近更新 更多