简体   繁体   中英

AlarmManager not repeating correctly

I try to have a notification repeated daily (For debugging I set it to every 10s). However, it is firing the notification only the first time, then nothing happens.

Here is the code where the alarm is set:

Intent myIntent = new Intent(ctx , NotifyService.class);
AlarmManager alarmManager =(AlarmManager)ctx.getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(ctx, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Calendar calendar = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 1000 * 10, pendingIntent); 

and here is the service:

public class NotifyService extends Service {
   public NotifyService() {

   }

   @Override
   public void onCreate(){
       //Create and Emit the notification.
   }

I have tried different flags in getService(ctx, int, Intent, flags) , to use setInexactRepeating and to set a new alarm after every call to the NotifyService .

Use a PendingIntent for a BroadcastReceiver rather than a Service , that is the recommended (and documented) practice. Plus, if you are using "wakeup" alarms, you'll need to use a BroadcastReceiver otherwise the system will not guarantee that it stays awake long enough for your Service to actually execute and receive the Intent . See this article for more details: http://po.st/7UpipA

Use the method below to repeating the alarm once in a day and you have to register broadcast receiver instead of service with AlarmManager so that you can start your service from the receiver and that is recommended.

Find the official doc .

     private final static String ACTION = "ACTION_ALARM";
     public static void setWakeUpAction(Context context, String hourSet, String minuteSet, String periodSet, int requestCode, String currentAction) {
            try {
                String mHour = hourSet;
                String mMin = minuteSet;
                String[] parsedFormat = null;
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
                SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
                Date date = parseFormat.parse(mHour + ":" + mMin + " " + periodSet);
                parsedFormat = displayFormat.format(date).split(":");
                mHour = parsedFormat[0];
                mMin = parsedFormat[1];
                calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(mHour));
                calendar.set(Calendar.MINUTE, Integer.parseInt(mMin));
                calendar.set(Calendar.SECOND, 00);
                Intent myIntent = new Intent(context, MyReceiver.class);
                myIntent.putExtra(ACTION, currentAction);
                myIntent.putExtra("Time", new String[]{mHour, mMin, periodSet});
                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pendingIntent);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

BroadCastReceiver

public class MyReceiver extends BroadcastReceiver {

    private final String ACTION = "ACTION_ALARM";
    private String ACTION_ONE = "ALARM_REPEAT";

    @Override
    public void onReceive(Context context, Intent intent) {
       try {
            String action = intent.getStringExtra(ACTION);
            new ShowToast(context, action);
            if (action.length() > 1) {
                if (action.equals(ACTION_ONE) ) {
                  String time[] = intent.getStringArrayExtra("Time");
                  startService(context, action);
                }
            }
        } catch (Exception e) {
        }

    }

    public void startService(Context context, String action) {
        Intent service1 = new Intent(context, NotifyService.class);
        service1.putExtra(ACTION, action);
        context.startService(service1);
    }

}

Manifest

   <service
        android:name=".NotifyService"
        android:enabled="true" />

    <receiver android:name=".MyReceiver" />

Usage

setWakeUpAction(context, "11", "00","AM","0", "ALARM_REPEAT");

Try this,

alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

// setRepeating() lets you specify a precise custom interval--in this case,
// 10 seconds.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        1000 * 10, alarmIntent);

Refer : http://developer.android.com/training/scheduling/alarms.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM