0

I'm creating a radio streaming app; in order to keep the audio playing when the user is working with other apps, I know I need to use a foreground service to run the code pertinent to streaming the audio.

Currently, I have the following code in my MainActivity class:

@Override
protected void onStart() {
    super.onStart();
    if(playIntent==null){
        playIntent = new Intent(this, MusicService.class);
        bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
        startService(playIntent);
    }
}

and:

private ServiceConnection musicConnection = new ServiceConnection(){

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        MusicService.MusicBinder binder = (MusicService.MusicBinder)service;
        //get service
        musicSrv = binder.getService();
        musicBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        musicBound = false;
    }
};

I'm calling startService() in the onStart() of the MainActivity, but I know I should be calling startForeground(), and yet I can't get it working. I don't have any real need to communicate with the user after the MainActivity has been closed, since it's just a radio streaming app/the user doesn't need to be able to change or identify songs that are currently playing. Any suggestions for altering how my service is operating? Thanks.

ZygD
  • 22,092
  • 39
  • 79
  • 102
dsideb
  • 35
  • 1
  • 10
  • 1
    Where's the code that attempts to `startForeground`? – Dan S Jun 02 '15 at 01:00
  • Radio streaming as in the same thing as radio, but online, or it literally takes the broadcast from a radio station (with permission, presumably) and plays it? If it's the former (Like Pandora or Spotify Radio) it might be a good idea to identify the song that's playing; it's a nice feature. If it's the latter, and it's possible, you should, too; again, it's a nice feature to have. (No, this has naught to do with your question, but I thought I'd point it out anyway) – Nic Jun 02 '15 at 01:02
  • Why are you binding the service and also startService? It already starts when you bind it. Also, the startForeground() is called by the Service itself, Are you trying to start the Service with startForeground? – leandrocastelli Jun 02 '15 at 01:22
  • "I can't get it working" is kind of vague. What part isn't working? "Any suggestions for altering how my service is operating?" I don't see an implementation of your service. – Andreas Jun 02 '15 at 01:47
  • first you need to run `startService()` and then `startForeground()` . Also you should run `startForeground()` inside the service. – TomTsagk Jun 02 '15 at 02:54

0 Answers0