Domanda

When a pending intent is sent, does it create a new activity? What if I have an activity already running? Is there a way to specify an already running activity, and have a method in that activity run once I send the intent?

What I want to do is have a button in a notification bar that acts a "stop" button, which will call the stop method in the already running application.

È stato utile?

Soluzione

It depends on the Activity's declaration in the manifest, or the Intent flags that you include.

For example, if you use FLAG_ACTIVITY_SINGLE_TOP (or the activity has launchMode set to "singleTop") then onNewIntent() will be called on the existing activity instead of creating a new one.

In your example, you should pass an extra in the intent to indicate that you want to execute the "stop" action, then check for it in onNewIntent().

This is well explained in the official documentation about launch modes: http://developer.android.com/guide/components/tasks-and-back-stack.html#TaskLaunchModes

EDIT: However, since the ultimate objective was playing audio in background, using a Service is a more appropriate option. Check http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices

To control the service from the notification (i.e. play, pause, stop) you need to supply PendingIntents created from with startService().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top