Alarm Manager is not activating broadcast receiver?

社会主义新天地 提交于 2019-12-06 06:19:33

A good tutorial on using Broadcast receivers is given in http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html. In essence, your receiver doesn't declare what events it will receive. The declaration in the Manifest file needs something like:

 <receiver
    android:name="myPackage.AlarmReceiver"
    android:enabled="true" >
    <intent-filter>
            <action android:name="your.company.blah.mybroadcast" />
        </intent-filter>
</receiver>

And when you create the intent, it needs

Intent intent = new Intent();
intent.setAction("your.company.blah.mybroadcast");
// All the other things you want to put in the intent

I know that this has been answered already, but just for further reference as I ran into the same issue, make sure that your receiver tags are inside the tags, but not inside any other tag such as activity (that's exactly the issue I had).

Also it helps a great deal to check that your alarm has been successfully registered, to so run: adb shell dumpsys alarm

You just need to registered your receiver with alarm manager this will call broadcast after every 60 second:

Intent broadcastIntent = new Intent(YourActivity.this, AlarmReceiver.class);

        PendingIntent pi = PendingIntent.getBroadcast(this, 0, broadcastIntent, 0);

       AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

        long timeInterval = 60 * 1000;

        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), timeInterval, pi);

In Manifest:

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