【发布时间】: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 台设备上对此进行了测试。
【问题讨论】: