Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'startactivity called from non-activity context service' warning when launching intent from notification

I've a service which starts a notification with startForeground(), I want the notification to launch an activity on click.
The acitivty I want to launch defined as android:launchMode="singleTask" and usually runs before the service starts.
Here is my pending intent creation code :

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

When I click on the notification I get the following warning :

startActivity called from non-Activity context; 
forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent........

I've also tried getting the activity with this instead of getApplicationContext() but got the same warning.
How should I make this right ?

like image 803
SagiLow Avatar asked Nov 18 '13 18:11

SagiLow


People also ask

How can we call method in activity from non activity class?

use interface to communicate with activity from non activity class. create colorChange() in interface and get the instance of interface in non activity class and call that method.

How do you start a activity from a non activity class?

public Context call mcontext;<br> // ..... <br> Intent intent = new Intent(call mcontext, AboutActivity. class);<br> call mcontext. startActivity(intent);

What is FLAG_ activity_ new_ task?

The flags you can use to modify the default behavior are: FLAG_ACTIVITY_NEW_TASK. Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent() .

How do you start a classroom activity?

1.2. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.


1 Answers

Don't use Intent.FLAG_ACTIVITY_NEW_TASK for PendingIntent.getActivity. Better use FLAG_ONE_SHOT.

And try to use Context of Activity instead getApplicationContext().

like image 59
Val Avatar answered Oct 23 '22 06:10

Val