0

I creating an android app which basically contains 2 media players. One that plays music from phone memory & other that plays streams from internet(URLs).

Each media player is defined in its separate activity. I'm using Service to playback. (i.e. when the user presses the play button, a Service gets started & plays the media).

The MusicPlayer is the default activity of the application & is in the package com.example.musicplayer. Whereas, the stream player is in the different package com.example.streamplayer.

Issue: When the application starts & I select the stream player & press the play button, although the URL stream gets played but I've also noticed that onStartCommand of other mediaplayer i.e. the music player also gets called.

This means that Activity B is calling Service A, even if both are in different packages.

Code for Music player activity & service

package com.example.musicplayer;

public class MusicPlayerActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        ....
        Intent service = new Intent(this, MusicService.class);
        startService(service);
        ....
    }

}

package com.example.musicplayer;

public class MusicService extends Service {

    ....

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Toast.makeText(this, "Music Service onStartCommand()", Toast.LENGTH_LONG).show();       // This gets displayed even in the Stream Activty

        return START_STICKY;
    }
    ....

}

Code for stream activity & service:

package com.example.streamplayer;

public class StreamPlayer extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ....
        Intent service = new Intent(this, StreamService.class);
        service.putExtra("URL", url);
        startService(service);
        ....
    }
}

package com.example.streamplayer;

public class StreamService extends Service {

    protected void startStream() {
        MediaPlayer mPlayer = new MediaPlayer();
        mPlayer.reset();
        mPlayer.setDataSource(url);
        mPlayer.prepareAsync();
        mPlayer.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();

            }
        });
    }
}

Manifest.xml

Note: in manifest file I've also tried adding to specify which service is for which intent. (Not sure if it works like this only)

<service
    android:name=".MusicService"
    android:enabled="true" >
    <intent-filter>
        <action android:name="com.example.musicplayer.MusicPlayerActivity" />
    </intent-filter>
</service>

<service
    android:name="com.example.streamplayer.StreamService"
    android:enabled="true" >
    <intent-filter>
        <action android:name="com.example.streamplayer.StreamService" />
    </intent-filter>
</service>

Please suggest how to make sure that the wrong service should not get called?

Prequel to this problem: here

Thank You

Community
  • 1
  • 1
reiley
  • 3,759
  • 12
  • 58
  • 114

1 Answers1

0

Your manifest shows only one service, even though you've defined two, and the action you put in for the service you defined makes no sense (it doesn't match any action you've defined in code) and would be ignored since both Intents you're sending are explicit.

Do you have both services defined in your manifest?

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
  • I've defined both the services...question updated. And, can you please tell what should go under action. I'm little confused with this element. Thank You – reiley Feb 16 '13 at 05:21