奥利奥:如何听取解锁事件?

时间:2018-01-19 09:51:42

标签: android

我想在用户解锁手机时做一些工作

我在AndroidManifest.xml

中定义了一个接收器
    <receiver
        android:enabled="true"
        android:name=".service.ScreenReceiver">
        <intent-filter>
            <action android:name="android.intent.action.USER_PRESENT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

和接收者

class ScreenReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {

}

private fun checkClearSavedNote(context: Context) {
    AppPref.getInstance(context).putString(AppPref.KEY_ID_CURRENT_NOTE, "")
    Log.e("Quang", "clear note")
}
}

但是在解雇时没有调用

我已尝试在

中使用ServiceregisterBroadcastReceiver

并在应用程序启动时启动它

class MyApplication : MultiDexApplication() {

override fun onCreate() {
    super.onCreate()
    instance = this
    MultiDex.install(applicationContext)
        try {
            startService(Intent(this, NoteService::class.java))
        } catch (e: IllegalStateException) {
            e.printStackTrace()
        }
}

} 但它只适用于Android API&lt; 8.0因为background execution limit

1 个答案:

答案 0 :(得分:0)

您可以将targetSdk < 26设置为利用隐式广播。否则,您必须根据最新的豁免广播列表更改您的设计。