简体   繁体   中英

AlarmManager does not fire PendingIntent - Android

I have made a class to set an AlarmManager and to receive it with a BroadcastReceiver, but it does not work. I tried already different types to set de AlarmManager, but nothing worked. Of course the BroadcastRecevier works fine if I call it in a other way.

public class AlarmBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent==null)
        {
            Log.d("INFO", "Intent is null");
        }

        if(context==null)
        {
            Log.d("INFO", "Context is null");
        }

        if(intent!=null && context!=null)
        {
            Log.d("INFO", "AlarmManager fired...");
        }
    }

    public static void startAlarm(Context context)
    {
        if(PendingIntent.getBroadcast(context, 0, new Intent(context, AlarmBroadcastReceiver.class), PendingIntent.FLAG_NO_CREATE)==null)
        {
            Log.d("INFO", "AlarmManager set...");
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), 10000, PendingIntent.getBroadcast(context, 0, new Intent(context, AlarmBroadcastReceiver.class), 0));
        }
    }
}

I hope you can help me! Thanks :D

You are using RTC_WAKEUP , but your time base is expressed as SystemClock.elapsedRealtime() . Those do not match. Either use ELAPSED_REALTIME_WAKEUP or change your time for the event to be based off of System.currentTimeMillis() or things that use it (eg, java.util.Calendar ).

Beyond that, confirm that your AlarmBroadcastReceiver is registered in the manifest, and see if you have any LogCat messages related to your problem.

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