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);
try
{
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?
When you want to open an activity within your app then you can call the startActivity() method with an Intent as parameter. That intent would be the activity that you want to open. First you have to create an object of that intent with first parameter to be the context and second parameter to be the targeted activity class.
Hope this will help.
You didn't paste the part where you call
startActivity
, that's the interesting part.You might be calling
startActivity
in aService
context, or in anApplication
context.Print "this" to log cat before making the
startActivity
call, and see what it refers to, it's sometimes a case of using an inner "this" accidentally.For Multiple Instance of the same activity , use the following snippet,
Note : This snippet, I am using outside of my
Activity
. Make sure yourAndroidManifest
file doesn't containandroid:launchMode="singleTop|singleInstance"
. if needed, you can change it toandroid:launchMode="standard"
.This works fine for me. Hope, this saves times for someone. If anybody finds a better way, please share with us.
Android Doc says -
That means for
(Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
it is mandatory to addIntent.FLAG_ACTIVITY_NEW_TASK
while callingstartActivity()
from outside of anActivity
context.So it is better to add flag for all the versions -
Sometimes this error can occur without an explicit call to
startActivity(...)
. For example, some of you may have seen a stack trace like this in Crashlytics:And you may wonder what you did wrong, since the trace only includes framework code. Well, here's an example of how this can happen. Let's say we're in a fragment.
Now, when a user clicks on that text view, your app will crash with the stack trace above. This is because the layout inflater has a reference to the application context, and so therefore your text view has an application context. Clicking on that text view implicitly calls
appContext.startActivity(...)
.Final note: I tested this on Android 4, 5, 6, and 7 devices. It only affects 4, 5, and 6. Android 7 devices apparently have no trouble calling
appContext.startActivity(...)
.I hope this helps someone else!
Try changing to this line: