AlarmManager没有及时执行该类? [英] AlarmManager is not executing the class in time?

查看:199
本文介绍了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来自动启动服务.这是一个很好的例子 https://stackoverflow.com/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 https://stackoverflow.com/a/4562747/5870896

这篇关于AlarmManager没有及时执行该类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆