0

我使用 scheduleAlarm 来触发BroadcastReceiver哪个 StartJobIntentService它工作正常,但是当我检查我得到的代码时,UnsafeProtectedBroadcastReceiver我添加了 if 条件检查onReceive方法来填充操作但JobIntentService没有工作。但如果我更换了条件,它就可以正常工作。请注意,我正在使用 android 9.any 帮助提前感谢 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"/>

工作意向服务

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

1 回答 1

1

当您将检查添加到您的 时,由 触发onReceive()的广播被忽略。这是因为您在 中检查了 ACTION并且您没有在您发送 via中设置 ACTION 。IntentAlarmManagerIntentIntentAlarmManager

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

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)
于 2019-04-02T14:08:56.747 回答