Calling startActivity() from outside of an Activity?


I'm using an AlarmManager to trigger an intent that broadcasts a signal. The following is my code:

AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, Wakeup.class);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
    Long elapsed +=  // sleep time;
    mgr.set(AlarmManager.RTC_WAKEUP, elapsed, pi);
catch(Exception r)
    Log.v(TAG, "RunTimeException: " + r);

I'm calling this code from an Activity, so I don't know how I could be getting the following error...

ERROR/AndroidRuntime(7557): java.lang.RuntimeException: Unable to start receiver com.wcc.Wakeup: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
1/10/2016 11:54:04 AM

if your android version is below Android - 6 then you need to add this line otherwise it will work above Android - 6.

Intent i = new Intent(this, Wakeup.class);
1/5/2019 10:44:08 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow