메인 액티비티에서 서비스를 이용해 음악을 재생한 후
서브 액티비티에서 토글을 통해 OFF를 하면 음악은 정지가 됩니다
이후 메인 액티비티 갔다가 다시 서브액티비티 오면 토글은 여전히 OFF로 잘 저장되어 있습니다.
그러나 문제는 토글의 기능적인 OFF부분은 잘 저장이 되는데 이미지는 저장이 안됩니다.
( 토글이 OFF면 스피커 꺼짐, ON이면 스피커 켜짐 )
토글이 OFF인 것에 대해 스피커가 꺼져있는 아이콘으로 나와야 하는데
기능적인 부분은 잘 되지만 이미지부분은 저장되지 않네요 ㅠㅠ 이 문제를 질문드립니다.
DialogActivity.java
public class DialogActivity extends AppCompatActivity {
public Boolean isServiceRunning(String BgmService) {
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo runningServiceInfo : activityManager.getRunningServices(Integer.MAX_VALUE)) {
System.out.println(runningServiceInfo.service.getClassName());
if (BgmService.equals(runningServiceInfo.service.getClassName())) {
return true;
}
}
return false;
}
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
final ToggleButton tb = (ToggleButton)this.findViewById(R.id.toggleButton);
tb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isServiceRunning("org.cheerluv.onoffmenu.BgmService") == true){
tb.setBackgroundDrawable(
getResources().
getDrawable(R.drawable.volume_mute_white_24dp));
Intent intent = new Intent(
getApplicationContext(), //현재제어권자
BgmService.class); //이동할 컴포넌트
stopService(intent); //서비스 시작
}
else{
tb.setBackgroundDrawable(
getResources().
getDrawable(R.drawable.volume_up_white_24dp));
Intent intent = new Intent(
getApplicationContext(), //현재제어권자
BgmService.class); //이동할 컴포넌트
startService(intent); //서비스 시작
}
}
});
BgmService.java
@Override
public void onCreate() {
super.onCreate();
// 서비스에서 가장 먼저 호출됨(최초에 한번만)
mp = MediaPlayer.create(this, R.raw.bgm);
mp.setLooping(false); // 반복재생
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 서비스가 호출될 때마다 실행
mp.start(); // 노래 시작
return super.onStartCommand(intent, flags, startId);
}
}