我使用 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)
}