1

i have a service class which controls my media playback, but i was wondering if using a seperate getters/setters class in my Service was bad.

In my fragment which displays all my songs, when i click a song i call the setter methods setSongIndex(); setSongList();.

Then in my Service class i call the getter methods in my playSong() method:

songList = Song.getSongList();
songIndex = Song.getSongIndex();

And on click next/previous song i call the setter methods again to replace the songIndex.

Now what i'm not sure about is that eventually my app will crash i think because when i swipe away my app from recents it will destroy all classes except my Service which runs in the background.

So calls to my Getters/setters methods won't be received because my app is killed and only the service is still running.

Weird thing is, my app works perfectly fine and i killed it from the recent apps list and can still call next/previous song from the notification calling the setters/getters?

Getters/setters class

public static void setSongIndex(int index){
    songIndex = index;
}

public static void setSongList(ArrayList<Song> songs){
    songList = songs;
}

public static ArrayList<Song> getSongList(){
    return songList;
}

public static int getSongIndex(){
    return songIndex;
}

Service class

private void nextSong(){
         if (songIndex == songList.size() - 1) {
             songIndex = 0;
             Song.setSongIndex(songIndex);
             activeSong = songList.get(songIndex);
         }else{
             ++songIndex;
             Song.setSongIndex(songIndex);
             activeSong = songList.get(songIndex);
             Toast.makeText(getApplicationContext(), "You Clicked position: " + songIndex + " " +  songList.get(songIndex).getData(), Toast.LENGTH_SHORT).show();
         }
         stopSong();
         playSong();

         //Send broadcast update song info
         Intent UpdateSongBroadCastReceiver = new Intent(Constants.ACTIONS.BROADCAST_UPDATE_SONG);
         sendBroadcast(UpdateSongBroadCastReceiver);
    }

private void playSong(){
    songList = Song.getSongList();
    songIndex = Song.getSongIndex();
    Log.i(TAG,"songList: " + songList);
    activeSong = songList.get(songIndex);
    mediaPlayer.reset();
    try {
        mediaPlayer.setDataSource(activeSong.getData());
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (Exception e) {
        Log.e(TAG, "ERROR SETTING DATA SOURCE", e);
        Main.unbindService(getApplicationContext());
        stopSelf();
    }
    NotificationBuilder(PlaybackStatus.PLAYING);
}
Vince VD
  • 1,506
  • 17
  • 38
  • The short answer is no. Service is a special component and not a regular class. Yo might find this helpful. https://medium.com/@ankit_aggarwal/ways-to-communicate-between-activity-and-service-6a8f07275297 https://stackoverflow.com/questions/2463175/how-to-have-android-service-communicate-with-activity – Froyo Jun 27 '19 at 16:01
  • @Froyo So sending and getting data through an accessor in Service is bad? – Vince VD Jun 27 '19 at 16:52
  • Yes, you won't have the Service instance available all the time. In case, if android kills and restarts your service, the service instances that you will have will not be useful. – Froyo Jun 27 '19 at 19:25

1 Answers1

0

you answer in one line

It depends on your situation 

but Ideal use of Accessors (also known as getters and setters) in Repository or Data Access Object class.

you can read more about this on this link

Istiaque Hossain
  • 2,157
  • 1
  • 17
  • 28
  • So when my app is killed and only my service is running in the background, i can still access this class? – Vince VD Jun 27 '19 at 16:11