Alarm setExact() stops executing












-1















Currently I have a problem with the alarmManager on Android.



When i start my app, i call the method "startReminderAlarm". Now the alarm is triggered on the upcoming hour and minute 1. The alarm triggers the AppReceiver. The AppReceiver starts the alarm again for the next hour and minute 1 and after that does some code in the "doInBackground" method.



With this pattern the AppReceiver should be called exactly on each hour ( minute 1 ). But it seems to work for only a few intervals. I started the alarm yesterday and checked it today with adb shell dumpsys alarm. The alarm was not mentioned on the output ?



I have the following situation :




  1. I can't reproduce this problem in the emulator

  2. When i change the alarm to trigger each minute, it seems to work.


My Questions are :




  1. Any problems with my code ?

  2. Is there a logfile where i can see Exception that may occure during the background execution ?


My Code :



// Class AlarmStarter
public static void startReminderAlarm(Context context){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.HOUR, 1);
calendar.set(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 0);
Log.i(TAG,"Start reminder alarm on " +sdf.format(new Date(calendar.getTimeInMillis())));

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent reminderIntent = new Intent(context, AppReceiver.class);
PendingIntent reminderPendingIntent = PendingIntent.getBroadcast(context, 0, reminderIntent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), reminderPendingIntent);
}

// Class AppReceiver
public class AppReceiver extends BroadcastReceiver {

private static final String TAG = "AppReceiver";

@Override
public void onReceive(Context context, Intent intent) {
AlarmStarter.startReminderAlarm(context);
new notifyAsyncTask().execute(context);
}

private static class notifyAsyncTask extends AsyncTask<Context, Void, Void> {

private String CHANNEL_ID = "1a2b3c4d";

private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

@Override
protected Void doInBackground(Context... params) {
// DO SOME CODE
}
}
}









share|improve this question





























    -1















    Currently I have a problem with the alarmManager on Android.



    When i start my app, i call the method "startReminderAlarm". Now the alarm is triggered on the upcoming hour and minute 1. The alarm triggers the AppReceiver. The AppReceiver starts the alarm again for the next hour and minute 1 and after that does some code in the "doInBackground" method.



    With this pattern the AppReceiver should be called exactly on each hour ( minute 1 ). But it seems to work for only a few intervals. I started the alarm yesterday and checked it today with adb shell dumpsys alarm. The alarm was not mentioned on the output ?



    I have the following situation :




    1. I can't reproduce this problem in the emulator

    2. When i change the alarm to trigger each minute, it seems to work.


    My Questions are :




    1. Any problems with my code ?

    2. Is there a logfile where i can see Exception that may occure during the background execution ?


    My Code :



    // Class AlarmStarter
    public static void startReminderAlarm(Context context){
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.HOUR, 1);
    calendar.set(Calendar.MINUTE, 1);
    calendar.set(Calendar.SECOND, 0);
    Log.i(TAG,"Start reminder alarm on " +sdf.format(new Date(calendar.getTimeInMillis())));

    AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent reminderIntent = new Intent(context, AppReceiver.class);
    PendingIntent reminderPendingIntent = PendingIntent.getBroadcast(context, 0, reminderIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), reminderPendingIntent);
    }

    // Class AppReceiver
    public class AppReceiver extends BroadcastReceiver {

    private static final String TAG = "AppReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
    AlarmStarter.startReminderAlarm(context);
    new notifyAsyncTask().execute(context);
    }

    private static class notifyAsyncTask extends AsyncTask<Context, Void, Void> {

    private String CHANNEL_ID = "1a2b3c4d";

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected Void doInBackground(Context... params) {
    // DO SOME CODE
    }
    }
    }









    share|improve this question



























      -1












      -1








      -1








      Currently I have a problem with the alarmManager on Android.



      When i start my app, i call the method "startReminderAlarm". Now the alarm is triggered on the upcoming hour and minute 1. The alarm triggers the AppReceiver. The AppReceiver starts the alarm again for the next hour and minute 1 and after that does some code in the "doInBackground" method.



      With this pattern the AppReceiver should be called exactly on each hour ( minute 1 ). But it seems to work for only a few intervals. I started the alarm yesterday and checked it today with adb shell dumpsys alarm. The alarm was not mentioned on the output ?



      I have the following situation :




      1. I can't reproduce this problem in the emulator

      2. When i change the alarm to trigger each minute, it seems to work.


      My Questions are :




      1. Any problems with my code ?

      2. Is there a logfile where i can see Exception that may occure during the background execution ?


      My Code :



      // Class AlarmStarter
      public static void startReminderAlarm(Context context){
      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(System.currentTimeMillis());
      calendar.add(Calendar.HOUR, 1);
      calendar.set(Calendar.MINUTE, 1);
      calendar.set(Calendar.SECOND, 0);
      Log.i(TAG,"Start reminder alarm on " +sdf.format(new Date(calendar.getTimeInMillis())));

      AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      Intent reminderIntent = new Intent(context, AppReceiver.class);
      PendingIntent reminderPendingIntent = PendingIntent.getBroadcast(context, 0, reminderIntent, PendingIntent.FLAG_CANCEL_CURRENT);
      alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), reminderPendingIntent);
      }

      // Class AppReceiver
      public class AppReceiver extends BroadcastReceiver {

      private static final String TAG = "AppReceiver";

      @Override
      public void onReceive(Context context, Intent intent) {
      AlarmStarter.startReminderAlarm(context);
      new notifyAsyncTask().execute(context);
      }

      private static class notifyAsyncTask extends AsyncTask<Context, Void, Void> {

      private String CHANNEL_ID = "1a2b3c4d";

      private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

      @Override
      protected Void doInBackground(Context... params) {
      // DO SOME CODE
      }
      }
      }









      share|improve this question
















      Currently I have a problem with the alarmManager on Android.



      When i start my app, i call the method "startReminderAlarm". Now the alarm is triggered on the upcoming hour and minute 1. The alarm triggers the AppReceiver. The AppReceiver starts the alarm again for the next hour and minute 1 and after that does some code in the "doInBackground" method.



      With this pattern the AppReceiver should be called exactly on each hour ( minute 1 ). But it seems to work for only a few intervals. I started the alarm yesterday and checked it today with adb shell dumpsys alarm. The alarm was not mentioned on the output ?



      I have the following situation :




      1. I can't reproduce this problem in the emulator

      2. When i change the alarm to trigger each minute, it seems to work.


      My Questions are :




      1. Any problems with my code ?

      2. Is there a logfile where i can see Exception that may occure during the background execution ?


      My Code :



      // Class AlarmStarter
      public static void startReminderAlarm(Context context){
      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(System.currentTimeMillis());
      calendar.add(Calendar.HOUR, 1);
      calendar.set(Calendar.MINUTE, 1);
      calendar.set(Calendar.SECOND, 0);
      Log.i(TAG,"Start reminder alarm on " +sdf.format(new Date(calendar.getTimeInMillis())));

      AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      Intent reminderIntent = new Intent(context, AppReceiver.class);
      PendingIntent reminderPendingIntent = PendingIntent.getBroadcast(context, 0, reminderIntent, PendingIntent.FLAG_CANCEL_CURRENT);
      alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), reminderPendingIntent);
      }

      // Class AppReceiver
      public class AppReceiver extends BroadcastReceiver {

      private static final String TAG = "AppReceiver";

      @Override
      public void onReceive(Context context, Intent intent) {
      AlarmStarter.startReminderAlarm(context);
      new notifyAsyncTask().execute(context);
      }

      private static class notifyAsyncTask extends AsyncTask<Context, Void, Void> {

      private String CHANNEL_ID = "1a2b3c4d";

      private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

      @Override
      protected Void doInBackground(Context... params) {
      // DO SOME CODE
      }
      }
      }






      java android alarmmanager






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 10:32









      Fantômas

      32.7k156389




      32.7k156389










      asked Nov 20 '18 at 10:25









      inf3ctioninf3ction

      245129




      245129
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Check your device(or emulator) version for setExec() method.
          If the version is higher than 22, you should use setExactAndAllowWhileIdle() method.






          share|improve this answer


























          • changed it to setExactAndAllowWhileIdle() the alarm started succesfully but after a few hours the alarm does not show up in the adb shell dumpsys alarm output.

            – inf3ction
            Nov 20 '18 at 20:40



















          0














          There was no problem at all with the code. The problem was the power management software of my Huawei smartphone.



          As I found out, some Android versions from several companys ( Huawei, Samsung, Xiaomi ... ) have stricter powermanagement rules.



          If an app is forced quit by the user, the alarm will work for a few more hours. Then comes the powermenagement software from the device and kills every background service for apps that are not running.



          That's why it was always working in the emulator and not on my smartphone.



          Solution for my situation :




          1. Deactivate the powermanagement for my app

          2. Start the alarm again


          IF SOMEONE ELSE RUNS INTO THIS PROBLEM, CHECK YOUR POWERMANAGEMENT SETTING!



          Some helpfull adb commands in this case :



          Finding out if your app is stopped. The value should be false. That means the background tasks a running and it was not killed by the powermanagement software.



          adb shell dumpsys package $packageName |grep stopped


          Display the running alarms for your package and some additional information like how much time passed since the last run:



          adb shell dumpsys alarm |grep -C4 $packageName





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390924%2falarm-setexact-stops-executing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Check your device(or emulator) version for setExec() method.
            If the version is higher than 22, you should use setExactAndAllowWhileIdle() method.






            share|improve this answer


























            • changed it to setExactAndAllowWhileIdle() the alarm started succesfully but after a few hours the alarm does not show up in the adb shell dumpsys alarm output.

              – inf3ction
              Nov 20 '18 at 20:40
















            1














            Check your device(or emulator) version for setExec() method.
            If the version is higher than 22, you should use setExactAndAllowWhileIdle() method.






            share|improve this answer


























            • changed it to setExactAndAllowWhileIdle() the alarm started succesfully but after a few hours the alarm does not show up in the adb shell dumpsys alarm output.

              – inf3ction
              Nov 20 '18 at 20:40














            1












            1








            1







            Check your device(or emulator) version for setExec() method.
            If the version is higher than 22, you should use setExactAndAllowWhileIdle() method.






            share|improve this answer















            Check your device(or emulator) version for setExec() method.
            If the version is higher than 22, you should use setExactAndAllowWhileIdle() method.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 11:21









            Bishan

            7,09441129206




            7,09441129206










            answered Nov 20 '18 at 10:45









            eltoryn7eltoryn7

            686




            686













            • changed it to setExactAndAllowWhileIdle() the alarm started succesfully but after a few hours the alarm does not show up in the adb shell dumpsys alarm output.

              – inf3ction
              Nov 20 '18 at 20:40



















            • changed it to setExactAndAllowWhileIdle() the alarm started succesfully but after a few hours the alarm does not show up in the adb shell dumpsys alarm output.

              – inf3ction
              Nov 20 '18 at 20:40

















            changed it to setExactAndAllowWhileIdle() the alarm started succesfully but after a few hours the alarm does not show up in the adb shell dumpsys alarm output.

            – inf3ction
            Nov 20 '18 at 20:40





            changed it to setExactAndAllowWhileIdle() the alarm started succesfully but after a few hours the alarm does not show up in the adb shell dumpsys alarm output.

            – inf3ction
            Nov 20 '18 at 20:40













            0














            There was no problem at all with the code. The problem was the power management software of my Huawei smartphone.



            As I found out, some Android versions from several companys ( Huawei, Samsung, Xiaomi ... ) have stricter powermanagement rules.



            If an app is forced quit by the user, the alarm will work for a few more hours. Then comes the powermenagement software from the device and kills every background service for apps that are not running.



            That's why it was always working in the emulator and not on my smartphone.



            Solution for my situation :




            1. Deactivate the powermanagement for my app

            2. Start the alarm again


            IF SOMEONE ELSE RUNS INTO THIS PROBLEM, CHECK YOUR POWERMANAGEMENT SETTING!



            Some helpfull adb commands in this case :



            Finding out if your app is stopped. The value should be false. That means the background tasks a running and it was not killed by the powermanagement software.



            adb shell dumpsys package $packageName |grep stopped


            Display the running alarms for your package and some additional information like how much time passed since the last run:



            adb shell dumpsys alarm |grep -C4 $packageName





            share|improve this answer






























              0














              There was no problem at all with the code. The problem was the power management software of my Huawei smartphone.



              As I found out, some Android versions from several companys ( Huawei, Samsung, Xiaomi ... ) have stricter powermanagement rules.



              If an app is forced quit by the user, the alarm will work for a few more hours. Then comes the powermenagement software from the device and kills every background service for apps that are not running.



              That's why it was always working in the emulator and not on my smartphone.



              Solution for my situation :




              1. Deactivate the powermanagement for my app

              2. Start the alarm again


              IF SOMEONE ELSE RUNS INTO THIS PROBLEM, CHECK YOUR POWERMANAGEMENT SETTING!



              Some helpfull adb commands in this case :



              Finding out if your app is stopped. The value should be false. That means the background tasks a running and it was not killed by the powermanagement software.



              adb shell dumpsys package $packageName |grep stopped


              Display the running alarms for your package and some additional information like how much time passed since the last run:



              adb shell dumpsys alarm |grep -C4 $packageName





              share|improve this answer




























                0












                0








                0







                There was no problem at all with the code. The problem was the power management software of my Huawei smartphone.



                As I found out, some Android versions from several companys ( Huawei, Samsung, Xiaomi ... ) have stricter powermanagement rules.



                If an app is forced quit by the user, the alarm will work for a few more hours. Then comes the powermenagement software from the device and kills every background service for apps that are not running.



                That's why it was always working in the emulator and not on my smartphone.



                Solution for my situation :




                1. Deactivate the powermanagement for my app

                2. Start the alarm again


                IF SOMEONE ELSE RUNS INTO THIS PROBLEM, CHECK YOUR POWERMANAGEMENT SETTING!



                Some helpfull adb commands in this case :



                Finding out if your app is stopped. The value should be false. That means the background tasks a running and it was not killed by the powermanagement software.



                adb shell dumpsys package $packageName |grep stopped


                Display the running alarms for your package and some additional information like how much time passed since the last run:



                adb shell dumpsys alarm |grep -C4 $packageName





                share|improve this answer















                There was no problem at all with the code. The problem was the power management software of my Huawei smartphone.



                As I found out, some Android versions from several companys ( Huawei, Samsung, Xiaomi ... ) have stricter powermanagement rules.



                If an app is forced quit by the user, the alarm will work for a few more hours. Then comes the powermenagement software from the device and kills every background service for apps that are not running.



                That's why it was always working in the emulator and not on my smartphone.



                Solution for my situation :




                1. Deactivate the powermanagement for my app

                2. Start the alarm again


                IF SOMEONE ELSE RUNS INTO THIS PROBLEM, CHECK YOUR POWERMANAGEMENT SETTING!



                Some helpfull adb commands in this case :



                Finding out if your app is stopped. The value should be false. That means the background tasks a running and it was not killed by the powermanagement software.



                adb shell dumpsys package $packageName |grep stopped


                Display the running alarms for your package and some additional information like how much time passed since the last run:



                adb shell dumpsys alarm |grep -C4 $packageName






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 9:46

























                answered Nov 22 '18 at 9:41









                inf3ctioninf3ction

                245129




                245129






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390924%2falarm-setexact-stops-executing%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings