DATE_CHANGED的广播接收器不适用于TIMEZONE_CHANGED

时间:2019-03-05 21:57:57

标签: android kotlin

我无法让自己的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种设备上进行了测试。

3 个答案:

答案 0 :(得分:1)

我暗中注册了broacastreceiver以进行日期更改..这对我有用。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

通过切换到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