0

Im using scheduledAlarm to trigger BroadcastReceiver which Start JobIntentService it worked fine but when i inspected the code i got UnsafeProtectedBroadcastReceiver I added if condition check in onReceive method to fillter the action but JobIntentService didn't work. but if I replaced the condition it worked fine. Note that I'm using android 9.any help thanks in advance 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
    }
}

Mainfest file

  <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)
        }
    }

alarm

  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 Answers1

1

When you add the check to your onReceive(), the broadcast Intent that was triggered by the AlarmManager got ignored. This is because you check for the ACTION in the Intent and you didn't set the ACTION in the Intent you send via AlarmManager.

To fix this, just add the 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)
David Wasser
  • 93,459
  • 16
  • 209
  • 274