서비스 공부중에 좀 모르는 부분이 생겨서 질문 드리게 되었습니다.
아래 소스가 문제의 소스의 액티비티 부분이구요...
=======================ServiceTest=======================
public class ServiceTest extends Activity {
private Button mButtonMessage;
private Intent mIntent;
private MyService serviceBinder;
private ServiceConnection mConnection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
serviceBinder = ((MyService.MyBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mIntent = new Intent(ServiceTest.this, MyService.class);
startService(mIntent);
bindService(mIntent, mConnection, Context.BIND_AUTO_CREATE);
mButtonMessage = (Button)findViewById(R.id.btn_message);
mButtonMessage.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(mIntent);
unbindService(mConnection);
}
});
}
}
=====================MyService===========================
public class MyService extends Service{
public NotificationManager mNM;
public MediaPlayer mMediaPlayer;
private final IBinder binder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
//mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Log.d("MyService", "onCreate()");
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
String text = "now something music playing";
Notification notification = new Notification(android.R.drawable.stat_sys_headset, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ServiceTest.class), 0);
notification.setLatestEventInfo(this, "label", text, contentIntent);
mNM.notify(1234, notification);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.d("MyService", "onStart");
mMediaPlayer = MediaPlayer.create(this, R.raw.afterschool);
if(mMediaPlayer.isPlaying() == false){
mMediaPlayer.start();
}
mMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.stop();
mp.release();
Log.d("MyService", "onCompletion()");
mNM.cancel(1234);
}
});
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("MyService", "onDestroy()");
mMediaPlayer.stop();
mNM.cancel(1234);
}
public class MyBinder extends Binder{
MyService getService(){
return MyService.this;
}
}
}
이렇게 만들었습니다.
실행 시키고 그 상태에서 버튼을 누르면 서비스도 잘 종료 되고 괜찮습니다.
그런데 back버튼을 누르고 안드로이드 메인화면으로 나오고 난뒤 notificationbar에서 클릭 해서 들어가면 노래가 중복 되어서 또다른 새로운 서비스가 실행 되는 것 같습니다.
그리고 바인드를 시켰는데 아래와 같은 에러가 납니다.
뭐 때문에 이런 에러가 나는지 감도 못잡고 있습니다. 도와주세요...
02-03 08:36:40.484: ERROR/ActivityThread(10378): Activity com.android.servicetest.ServiceTest has leaked ServiceConnection com.android.servicetest.ServiceTest$1@43bb8fc8 that was originally bound here
02-03 08:36:40.484: ERROR/ActivityThread(10378): android.app.ServiceConnectionLeaked: Activity com.android.servicetest.ServiceTest has leaked ServiceConnection com.android.servicetest.ServiceTest$1@43bb8fc8 that was originally bound here




bind한 후에 해제하지 않고 다시 bind하면 저 에러가 납니다. back key를 오버라이딩해서 back key시에 unbind처리를 하시면 됩니다.