Search code examples
androidbroadcastreceiveralarmmanagerandroid-pendingintentintentservice

BroadcastReceiver doesn't start activity when app closed


So, I've been trying to achieve that for at least five hours today and I've tried literally ANY solution found anywhere.

I'm programming a little clock app, using AlarmManager to make the app ring. It works fine when my app is open or minimized. I'm trying to make it work when the app is closed, and that's the problem. So, there is the piece of code that sets the AlarmManager:

    AlarmManager am = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(ctxt, AlarmService.class);
    PendingIntent pen = PendingIntent.getBroadcast(ctxt, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    am.setExact(AlarmManager.RTC_WAKEUP, nextRing.getTime(), pen);

(Here, ctxt is the context, I've tried both getApplicationContext() and getBaseContext() and nextRing.getTime() is a long that represents the date)

Then, I have my AlarmService class (which used to be a service, which explain the name, but is now a BroadcastReceiver and I just don't want to rename it now)

public class AlarmService extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {

    /*Intent cust = new Intent(context, CustomIntent.class);
    context.startService(cust);*/
    Bundle extras = intent.getExtras();
    Intent newIntent = new Intent(context, AlarmActivity.class);
    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    newIntent.putExtra("AlarmName", extras.getString("AlarmName"));
    context.startActivity(newIntent);
  }

}

So this is the try with only the BroadcastReceiver, which doesn't work obviously, so I tried to add a IntentService (commented out code at the top) which has the following code

public class CustomIntent extends IntentService {
  public CustomIntent() {
    super("CustomIntent");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    Intent newIntent = new Intent(getBaseContext(), AlarmActivity.class);
    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getApplication().startActivity(newIntent);
  }
}

...and that's not working either! Finally, here's the manifest I use:

<application>
[Here goes stuff that have nothing to do with my bug]
    <receiver android:name=".custom_classes.AlarmService"/>
    <service
      android:name="com.group9.abclock.custom_classes.CustomIntent"
      android:enabled="true" >
      <intent-filter>
        <action android:name="com.group9.abclock.custom_classes.CustomIntent" />
      </intent-filter>
    </service>
  </application>

Sorry for the (very) long post but I thought I should explain anything I tried. Thanks in advance if you can help!


Solution

  • If your application is closed, you can't trigger a BroadcastReceiver because it's not registered, and you can't use Context methods because there is no Context.

    If you want to execute tasks while the application is closed, you have to create another project with a Service, and start it with your application.

    A Service runs in background until someone kill it, and once it's started, the main application is not needed anymore. So the ring functionality have to be implemented in this service.

    Just remember that the AlarmManager.setExact, is not that exact from API 23, due to Doze Mode.