AlarmManager会立即启动应用程序,并且永远不会在睡着时触发

时间:2019-06-15 01:39:22

标签: java android kotlin broadcastreceiver alarmmanager

我正在创建一个用于管理提醒和警报的应用程序,我已经使用该应用程序约15天了,没有任何问题,但是目前AlarmManger不会在确切的时间启动,并且永远不会启动我手动打开该应用程序。我没有更改有关AlarmManagerBroadcastReceivers的任何代码,所以这是我的代码:

  • 主要使用syncTask()同步 * AlarmManager 的项目:

    object RemindersManager {
    
    private var alarmManager : AlarmManager? = null
    private var alarmIntent : PendingIntent? = null
    
    fun syncTask(context: Context, task: TaskData) {
        if (task.id != null) {
            prepareObjects(context, task)
        }
    }
    
    private fun prepareObjects(context: Context, task: TaskData) {
        if (alarmManager == null) {
            alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        }
    
        alarmIntent = Intent(IntentActions.REMINDER_RECEIVED).let { intent ->
            intent.setClass(context, AlarmReceiver::class.java)
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
            intent.putExtra(DataKeys.TASK_ID, task.id!!)
            PendingIntent.getBroadcast(context, task.id!!.toInt() , intent, PendingIntent.FLAG_ONE_SHOT)
        }
        if (task.reminderDate != null) {
            attachTask(task)
        } else {
            cancelTask()
        }
    
    }
    
    private fun attachTask(task : TaskData) {
        if (!DateUtils.hasDatePassed(task.reminderDate)) {
            when {
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> {
                    alarmManager?.setExactAndAllowWhileIdle(
                        AlarmManager.RTC_WAKEUP,
                        task.reminderDate!!.time,
                        alarmIntent
                    )
                }
                Build.VERSION.SDK_INT >= 21 -> {
                    alarmManager?.setExact(
                        AlarmManager.RTC_WAKEUP,
                        task.reminderDate!!.time,
                        alarmIntent
                    )
                }
            }
        }
    }
    
    private fun cancelTask() {
        alarmManager?.cancel(alarmIntent)
        alarmIntent?.cancel()
    }
    }
    
  • 接收董事会直播

    class AlarmReceiver : BroadcastReceiver() {
    
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == IntentActions.REMINDER_RECEIVED) {
            val taskId = intent.getLongExtra(DataKeys.TASK_ID, -1)
            with(TaskDatabase(context)) {
                getItem(taskId)?.apply {
                    when (reminderType) {
                        TaskData.REMINDER_NOTIFICATION -> {
                            TaskNotification.showNotification(context, this)
                        }
    
                        TaskData.REMINDER_ALARM -> {
                            context.startActivity(
                                Intent(context, AlarmActivity::class.java).apply {
                                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                    addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
                                    putExtra(DataKeys.TASK_ID, taskId)
                                })
                        }
                    }
                    RepeatingAsyncTask(context).execute(this)
                    close()
                }
            }
        }
    }
    }
    
  • 注册广播

    <receiver
            android:exported="false"
            android:name=".receivers.AlarmReceiver"
            android:enabled="true">
        <intent-filter>
            <action android:name="com.tamimattafi.app.REMINDER_RECEIVED"/>
        </intent-filter>
    </receiver>
    

0 个答案:

没有答案