AlarmManager没有及时执行该类?

编程入门 行业动态 更新时间:2024-10-28 03:28:32
本文介绍了AlarmManager没有及时执行该类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的项目中,我想每天在特定时间更改SharedPreference中的标志值,我实现了AlarmManager,但未执行任务. 我调用接收方类的函数:

In my project I want to change a flag value in SharedPreference in particular time every day ,I have implemented the AlarmManager but It is not performing the task . My function to call my receiver class :

public void changeAttendaceFlag(){ Log.d(TAG,"changeAttendaceFlag !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY,14); calendar.set(Calendar.MINUTE,23); calendar.set(Calendar.SECOND,10); Intent activateLogin = new Intent(getApplicationContext(),Attendance.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),101,activateLogin,PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent); }

我的接收器类:

public class Attendance extends BroadcastReceiver { FcmSession fcmSession; private static final String TAG = "Attendance"; @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, " Attendance Called !!!!!!!!!!!!!!!!!!!!", Toast.LENGTH_SHORT).show(); fcmSession = new FcmSession(context); fcmSession.store_dialog_value(true); UtilsMethods utilsMethods = new UtilsMethods(); String time = utilsMethods.getCurrentDateAndTime(); Log.d(TAG,"change attendance flag :"+time); } }

推荐答案

这只是答案的一部分.这是实现服务onStartCommand

This is just part of the answer. Here is how you implement the service onStartCommand

@Override public int onStartCommand(Intent intent, int flags, int startId) { // this is where you run your changeAttendaceFlag() method return START_STICKY; // this will make your service recreate when your app is closed }

在您的班级扩展Service

START_STICKY :使用此返回值将使您在关闭应用程序时重新创建服务.当您强制关闭应用程序时,将重新创建该服务,但不会重新提供该意图,因此该服务将始终在不做任何事情的情况下运行.

START_STICKY : Using this return value will make your service recreate when you close your app. When you force close your app the service will recreated but the intent wont be redeliver therefore the service will always run without doing anything.

START_NOT_STICKY :使用此返回值将使您的服务在关闭时不会重新创建.

START_NOT_STICKY : Using this return value will make your service not recreate when closed.

START_REDELIVER_INTENT :使用此返回值将使您的服务功能类似于START_STICKY,但是这一次有意图时,它将重新交付它.

START_REDELIVER_INTENT : Using this return value will make your service function similar to START_STICKY but this time when theres an intent, it will redeliver it.

注意:应用程序关闭后,由于将重新创建服务,因此将重新实例化数据.将数据保存在 SharedPreferences 或 SQLite DB 中.

Note: Data will be reinstantiate once app is closed since the service will be recreated. Save your data in SharedPreferences or SQLite DB.

对于自动启动,您可以在设置中执行此操作,但是当您重新启动手机时,它将禁用自动启动,您需要启用它,这确实是非常糟糕的用户体验(我认为这是针对某些手机的).您还可以通过为 BOOT_COMPLETED 实施BroadcastReceiver来自动启动服务.这是一个很好的例子 stackoverflow/a/4562747/5870896

For autostart, you can do that in settings but when you restart your phone, it will disable autostart and you need to enable it which is a really bad user experience(I think this is for some phone). You can do autostart on your service as well by implementing a BroadcastReceiver for BOOT_COMPLETED. Here is a great example stackoverflow/a/4562747/5870896

更多推荐

AlarmManager没有及时执行该类?

本文发布于:2023-11-27 14:43:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638499.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!