我有一个控制媒体播放的服务类,但是我想知道在我的服务中使用单独的getters/setters类是否不好。
在显示我所有歌曲的片段中,当我单击一首歌时,我称为setter方法setSongIndex(); setSongList();
。
然后在我的服务类中 单击下一步/上一首歌曲,我再次调用Setter方法以替换SongIndex。 现在,我不确定的是,最终我的应用程序会崩溃,因为当我将应用程序从后备身上刷掉时,它将摧毁所有类,除了我的服务在后台运行。 因此,由于我的应用程序被杀死,而且服务仍在运行。 很奇怪的是,我的应用程序正常工作,我从最近的应用程序列表中杀死了它,并且仍然可以从通知召唤播放器/getters的Next/上一首歌?songList = Song.getSongList();
songIndex = Song.getSongIndex();
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;
}
服务类
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);
}
您在一行中回答
It depends on your situation
但是,在存储库或数据访问对象类中的登录器(也称为Geters and Setter(的理想用途。
您可以在此链接上阅读有关此信息的更多信息