Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – AlarmManager setEexact()无效大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_616_2@
我有两个AlarmManager计划每天23:59发射一次,另一个同时发射,但每周发射一次.

以下是执行此操作的两个函数

private fun midnightTimeClearCache(dataType: DataType,requestCode: int) {
        val calendar = Calendar.geTinstance()
        calendar.timeInMillis = System.currentTimeMillis()
        calendar.set(Calendar.HOUR_OF_DAY,23)
        calendar.set(Calendar.minutE,59)
        val intent = Intent(context,ClearDataReceiver::class.java)
        intent.putExtra(ClearDataReceiver.DATA_TYPE_EXTRA,dataType.Name)
        val pendingIntent = PendingIntent.getBroadcast(context,requestCode,intent,PendingIntent.FLAG_updatE_CURRENT)
        alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.timeInMillis,pendingIntent)

    }

    private fun weekTimeClearCache(dataType: DataType,59)
        var i: Int = calendar.get(Calendar.WEEK_OF_MONTH)
        calendar.set(Calendar.WEEK_OF_MONTH,i++)
        val intent = Intent(context,pendingIntent)
    }

以下是作为上述待处理意图的一部分应该被解雇的接收者

class ClearDataReceiver : DaggerBroadcastReceiver() {
 override fun onReceive(context: Context?,intent: Intent?) {
        super.onReceive(context,intent)
        var bundle = intent!!.extras
        val dataTypeString = bundle.getString(DATA_TYPE_EXTRA)
        val dataType = DataType.valueOf(dataTypeString)
        Log.d("JJJ","clearing data for " + dataTypeString)
        when (dataTypE) {
            DataType.CUSTOMER_DETAILS -> {
                storage.clearDetails()
                storageClearschedular.setCustomerDetailsClear()
            }
            DataType.CUSTOMER_PIC -> {
                storage.clearPic()
                storageClearschedular.setPicClear()
            }
}

这是我的清单:

@R_801_5179@ 
@R_801_5179@     
@R_801_5179@@R_801_5179@  
@R_801_5179@     
@R_801_5179@ 

和启动接收器,@R_417_9447@重置alerm管理器

override fun onReceive(context: Context?,intent: Intent?) {
        Log.d("JJJ","onReceive")

        storageClearschedular = StorageClearschedular(context!!,context!!.getSystemservice(Context.ALARM_serviCE) as AlarmManager)
        storageClearschedular.setAllschedulars()

当我设置上述警报管理器时间表然后在我睡觉之前从后台杀死应用程序时,我再次加载应用程序以发现它没有执行我的广播接收器(没有关于日志和数据的详细信息未清除).

应该发生的是它应该在我设置的时间或每周一次执行每个数据,然后在它被触发后再次设置它但它似乎根本不起作用.

我首先尝试了setRepeaTing,但是它确实起到了api 19及以上版本的im定位设备的作用

我错过了什么?

@H_616_2@

解决方法

对于19以上的api版本,请使用此代码

if (Build.VERSION.SDK_INT < 19) {


           alarmManager.set(AlarmManager.RTC,calendar.getTimeInMillis(),pendingIntent);
             }
        } else if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT <= 22) {
            alarmManager.setExact(AlarmManager.RTC,pendingIntent)
        } else if (Build.VERSION.SDK_INT >= 23) {
            alarmManager.setExactAndAllowWhilEIDle(AlarmManager.RTC,pendingIntent)
        }

对于每日警报,您可以检查状况.此示例将在每日上午9点设置警报.此外,您可能需要在设备重新启动或启动时再次设置警报,因为一旦设备关闭或重新启动,所有警报都会被取消.

Calendar calendar = Calendar.geTinstance()
        // set SELEcted time from timepicker to calendar
        calendar.setTimeInMillis(System.currentTimeMillis());

        // if it's after or equal 9 am schedule for next day
        if (Calendar.geTinstance().get(Calendar.HOUR_OF_DAY) >= 9) {
            Log.e("","Alarm will schedule for next day!");
            calendar.add(Calendar.DAY_OF_YEAR,1); // add,not set!
        }
        else{
            Log.e("","Alarm will schedule for today!");
        }
        calendar.set(Calendar.HOUR_OF_DAY,9);
        calendar.set(Calendar.minutE,0);
        calendar.set(Calendar.SECOND,0);
@H_616_2@ @H_616_2@
@H_616_2@
@H_616_2@

大佬总结

以上是大佬教程为你收集整理的android – AlarmManager setEexact()无效全部内容,希望文章能够帮你解决android – AlarmManager setEexact()无效所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。