4

I'm struggling on an app that must repeat a task with a specified interval. I want it to wakeup the device if needed. I have no idea why, but the WakefulBroadcastReceiver NEVER executes its onReceive method that should be triggered via AlarmManager. The problem persists with a normal BroadcastReceiver. I'm using a Lollipop 5.0.1 Nexus5, and time intervals are of 5-20 seconds.

Here's the code:

class mypackage.MainActivity

[...]
private void startRepeatingAlarm(){
    this.wbr = new SimpleWakefulBroadcastReceiver();
    this.registerReceiver(wbr, new IntentFilter("mypackage.FOO_ACTION"));
    Intent i = new Intent("mypackage.FOO_ACTION");
    i.setClass(this, SimpleWakefulBroadcastReceiver.class);

    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 1234, i, PendingIntent.FLAG_CANCEL_CURRENT);      
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);      
    manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), timeInterval, pendingIntent);
    Toast.makeText(this, "Alarm set", Toast.LENGTH_SHORT).show();
}

class mypackage.SimpleWakefulBroadcastReceiver

public class SimpleWakefulBroadcastReceiver extends WakefulBroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // This method gets NEVER called
    Intent service = new Intent(context, SimpleWakefulService.class);
    startWakefulService(context, service);
}}

2 Answers2

3

I'm going to stick my neck out here and guess that you haven't declared the broadcast receiver in your manifest:

<receiver android:name=".SimpleWakefulBroadcastReceiver"/>
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • It worked! But as far as I understood by reading the official documentation and from an Eclipse warning statement, it is not necessary to declare the receiver in the manifest if you do it programmatically---it is suggested to do so as a best practice and I do it in the startRepeatingAlarm() method. – Marco Virgolin Mar 13 '15 at 09:26
  • 1
    The documentation is sometimes misleading :-( You need to declare your receiver in the manifest if it will be accessed by an **external component** (ie: something outside of your application). Since you are using `AlarmManager` to trigger your receiver (and `AlarmManager` is an external component) you need to have the receiver declared in the manifest. – David Wasser Mar 13 '15 at 12:06
  • Other common error is to use PendingIntent.getService instead of PendingIntent.getBroadcast – vinga Feb 10 '16 at 19:16
-1
private void startRepeatingAlarm(){    
 Intent intent = new Intent(MainActivity.this, SimpleWakefulBroadcastReceiver.class);
                intent.setAction(Intent.ACTION_MAIN);
                pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
                        0, intent, 0);
              //repeat per 1 min
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1 * 60 * 1000, pendingIntent);

}
  • Welcome to Stack Overflow! Although this code may help to solve the problem, it doesn't explain _why_ and/or _how_ it answers the question. Providing this additional context would significantly improve its long-term educational value. Please [edit] your answer to add explanation, including what limitations and assumptions apply. – Toby Speight Oct 25 '16 at 09:56