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);
}