AlarmManager is not executing the class in time?

非 Y 不嫁゛ 提交于 2020-01-17 12:26:13

问题


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);
    }

My receiver class :

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);
    }
}

回答1:


Try to use Inexact instead of just simple Repeating for targetSdk greater than 19 API. Check Note at this LINK.

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

So, Change and try with below line:-

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

Updated:

Also, In some phone devices(like Xiaomi, Lenovo) app needs to put in AutoStart list. For reference answer check this link.




回答2:


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
}

The following return type will be available once your class extends Service

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 : Using this return value will make your service not recreate when closed.

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.

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

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



来源:https://stackoverflow.com/questions/45007981/alarmmanager-is-not-executing-the-class-in-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!