避免棉绒报告UnsafeProtectedBroadcastReceiver后,BroadcastReceiver将无法在android 9中工作

时间:2019-03-27 15:15:34

标签: android broadcastreceiver jobintentservice

我正在使用ScheduledAlarm来触发BroadcastReceiver,它启动了JobIntentService,它工作正常,但是当我检查代码时,我得到了UnsafeProtectedBroadcastReceiver,我添加了条件检查中的onReceive方法以进行填充该操作,但JobIntentService无效。但是如果我替换了条件,它就可以正常工作。请注意,我正在使用android 9。 BroadCastReceiver

class BootBroadCastReciever : BroadcastReceiver() {

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

 if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
         val i = Intent(context, IncomingRequestServices::class.java)
            //Start the service
            context?.let {
                IncomingRequestServices.enqueueWork(it,i)
        }
    }

    }

    companion object {
        const val REQUEST_CODE = 12345
    }
}

维护文件

  <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
   <receiver android:name=".BoardcastReciever.BootBroadCastReciever" android:exported="true">

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

        <service
          android:name=".Services.IncomingRequestServices"
          android:exported="false"
          android:permission="android.permission.BIND_JOB_SERVICE"/>

JobIntentService

class IncomingRequestServices  : JobIntentService() {

    var CHANNEL_ID = "ChannelID"


    override fun onHandleWork(intent: Intent) {
        //I check for news from server and send notification to user
    }




    companion object
    {
        const val JOB_ID = 101
        const val NOTIF_ID = 82
        fun enqueueWork(context: Context, work: Intent)
        {
            JobIntentService.enqueueWork(context,IncomingRequestServices::class.java, JOB_ID, work)
        }
    }

警报

  private fun scheduleAlarm() {
        // Construct an intent that will execute the AlarmReceiver
        val intent = Intent(applicationContext, BootBroadCastReciever::class.java)
        // Create a PendingIntent to be triggered when the alarm goes off
        alarmPendingIntent = PendingIntent.getBroadcast(this, BootBroadCastReciever.REQUEST_CODE,
                intent, PendingIntent.FLAG_UPDATE_CURRENT)
        // Setup periodic alarm every 5 seconds
        val firstMillis = System.currentTimeMillis() // first run of alarm is immediate
        val intervalMillis = 60000 // as of API 19, alarm manager will be forced up to 60000 to save battery
        val alarm = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        // See https://developer.android.com/training/scheduling/alarms.html
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, intervalMi

llis.toLong(), alarmPendingIntent)
        }

1 个答案:

答案 0 :(得分:1)

将支票添加到onReceive()时,由Intent触发的广播AlarmManager被忽略。这是因为您在Intent中检查了ACTION,而没有在通过Intent发送的AlarmManager中设置ACTION。

要解决此问题,只需添加ACTION:

val intent = Intent(applicationContext, BootBroadCastReciever::class.java)
intent.setAction(Intent.ACTION_BOOT_COMPLETED)
// Create a PendingIntent to be triggered when the alarm goes off
alarmPendingIntent = PendingIntent.getBroadcast(this, BootBroadCastReciever.REQUEST_CODE,
            intent, PendingIntent.FLAG_UPDATE_CURRENT)