Reputation: 281
I have no problem in passing data to a service using intent, extras, etc and using startService(intent)
Intent intent = new Intent();
intent.setClass(MainActivity.this, MediaPlayerService.class);
intent.setAction(MediaPlayerService.PLAY_PAUSE);
intent.putExtra("URI", URI);
intent.putExtra("TITLE", content.toString());
//PendingIntent.getService(MainActivity.this, 0, intent, 0);
startService(intent);
The problem is that the service is a background service using MediaPlayer to play music. Using startService each time I'm afraid that it might be spawning multiple players, and eventually I get an ANR (even if everything is working smoothly)
All I need is to tell the MusicPlayerService to play a new track. Any ideas? As you can see from the code I also tried PendingIntent (suggested on the net) but it didn't seem to work
thanks!
Upvotes: 1
Views: 936
Reputation: 281
Solved! I checked and if I did
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d(TAG,"super called "+startId);
...
I saw startId was constantly increasing and giving timeouts eventually. Solved by calling
stopService(intent)
...
startService(intent)
so simple and yet so many headaches to find it out!
Upvotes: 0
Reputation: 18151
Bind to your service and sent your message through Messenger.
Upvotes: 1
Reputation: 1007369
Using startService each time I'm afraid that it might be spawning multiple players
No. There will be precisely zero or one instance of your service. If the service is already started, startService()
does not start a second instance, but merely calls onStartCommand()
of the existing instance.
and eventually I get an ANR
The stack trace should point out the source of your difficulty.
Upvotes: 2