如何解决Android屏幕打开和屏幕关闭接收器在启动后不会触发?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (105)

AndroidManifest.xml中:

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

<application
    android:name=".MyApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" 

    <receiver
        android:name=".receivers.BootCompleteReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

    <receiver android:name=".receivers.ScreenOnReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_ON" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

    <receiver android:name=".receivers.ScreenOffReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_OFF" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

接受者:

class BootCompleteReceiver : BroadcastReceiver() {

    override fun onReceive(p0: Context, p1: Intent?) {
        if (p1?.action.equals(Intent.ACTION_BOOT_COMPLETED, true))
            Log.i(TAG, "Boot Time: %d".format(System.currentTimeMillis()))
    }
}

接受者:

class ScreenOnReceiver: BroadcastReceiver() {

    override fun onReceive(p0: Context, p1: Intent?) {
        if (p1?.action.equals(Intent.ACTION_SCREEN_ON, true))
            Log.i(TAG, "Screen On Time: %d".format(System.currentTimeMillis()))
    }
}

class ScreenOffReceiver: BroadcastReceiver() {

    override fun onReceive(p0: Context, p1: Intent?) {
        if (p1?.action.equals(Intent.ACTION_SCREEN_OFF, true))
            Log.i(TAG, "Screen Off Time: %d".format(System.currentTimeMillis()))
    }
}

MyApp:

private lateinit var screenOnReceiver: ScreenOnReceiver
private lateinit var screenOffReceiver: ScreenOffReceiver

override fun onCreate() {
    super.onCreate()

    val screenOnIntentFilter = IntentFilter(Intent.ACTION_SCREEN_ON)
    screenOnReceiver = ScreenOnReceiver()
    registerReceiver(screenOnReceiver, screenOnIntentFilter)

    val screenOffIntentFilter = IntentFilter(Intent.ACTION_SCREEN_OFF)
    screenOffReceiver = ScreenOffReceiver()
    registerReceiver(screenOffReceiver, screenOffIntentFilter)
}
提问于
用户回答回答于

我也有同样的问题,试试以下代码:

 <receiver
            android:name=".OnBoot"
            android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.REBOOT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

扫码关注云+社区

领取腾讯云代金券