简体   繁体   English

AlarmManager不会触发PendingIntent-Android

[英]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. 我已经创建了一个类来设置AlarmManager并通过BroadcastReceiver接收它,但是它不起作用。 I tried already different types to set de AlarmManager, but nothing worked. 我已经尝试了不同的类型来设置de AlarmManager,但是没有任何效果。 Of course the BroadcastRecevier works fine if I call it in a other way. 当然,如果我以其他方式调用它,BroadcastRecevier也可以正常工作。

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 感谢:D

You are using RTC_WAKEUP , but your time base is expressed as SystemClock.elapsedRealtime() . 您正在使用RTC_WAKEUP ,但是您的时基表示为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 ). 使用ELAPSED_REALTIME_WAKEUP或更改事件的时间,使其基于System.currentTimeMillis()或使用该事件的事物(例如, 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. 除此之外,请确认您的AlarmBroadcastReceiver已在清单中注册,然后查看是否有与问题相关的LogCat消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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