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 ?
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.
public Context call mcontext;<br> // ..... <br> Intent intent = new Intent(call mcontext, AboutActivity. class);<br> call mcontext. startActivity(intent);
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() .
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.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With