1

서비스에 바인딩 할 때 어떤 일이 생기는지 설명해 주시겠습니까?하지만 시작한 다음 언 바인딩하지 마십시오. "활동으로 인해 서비스 연결 오류가 유출되었습니다"라는 메시지가 표시되지만 이유를 이해할 수 없습니다.BIND_AUTO_CREATE를 사용하지 않을 때 ServiceConnection 누수가 발생했습니다.

MyService : package com.example.servicetest;

import android.app.Service; 
import android.content.Intent; 
import android.os.Binder; 
import android.os.IBinder; 

public class MyService extends Service{ 

    Binder mLocalBinder = new Binder(); 

    @Override 
    public IBinder onBind(Intent intent) { 

     return mLocalBinder; 
    } 

} 

MainActivity :

package com.example.servicetest; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button button1 = (Button) findViewById(R.id.button1); 
     button1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MainActivity.this, BoundActivity.class); 
       startActivity(intent); 

      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 

     return true; 
    } 

} 

BoundActivity :

package com.example.servicetest; 

import android.os.Bundle; 
import android.os.IBinder; 
import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.view.Menu; 

public class BoundActivity extends Activity { 

boolean mBound = false; 
ServiceConnection mConnection = new ServiceConnection() { 

    @Override 
    public void onServiceDisconnected(ComponentName name) { 
     mBound = false; 

    } 

    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 
     mBound = true; 
    } 
}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bound); 
} 

@Override 
protected void onResume(){ 
    super.onResume(); 
    Intent service = new Intent(this, MyService.class); 
    bindService(service, mConnection, 0); 

} 

@Override 

protected void onPause(){ 
    if (mBound){ 
     unbindService(mConnection); 
    } 
    super.onPause(); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.bound, menu); 
    return true; 
} 

}

그때 button1을 클릭하면, BoundActivity가 시작되고 그이면 MyService에 결합한다. 다시 클릭하면 다음 예외가 발생합니다.

10-19 21:50:17.445: E/ActivityThread(24138): Activity com.example.servicetest.BoundActivity has leaked ServiceConnection [email protected] that was originally bound here 
10-19 21:50:17.445: E/ActivityThread(24138): android.app.ServiceConnectionLeaked: Activity com.example.servicetest.BoundActivity has leaked ServiceConnection [email protected] that was originally bound here 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ContextImpl.bindServiceAsUser(ContextImpl.java:1452) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ContextImpl.bindService(ContextImpl.java:1440) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.content.ContextWrapper.bindService(ContextWrapper.java:496) 
10-19 21:50:17.445: E/ActivityThread(24138): at com.example.servicetest.BoundActivity.onResume(BoundActivity.java:37) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.Activity.performResume(Activity.java:5211) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.os.Looper.loop(Looper.java:137) 
10-19 21:50:17.445: E/ActivityThread(24138): at android.app.ActivityThread.main(ActivityThread.java:5103) 
10-19 21:50:17.445: E/ActivityThread(24138): at java.lang.reflect.Method.invokeNative(Native Method) 
10-19 21:50:17.445: E/ActivityThread(24138): at java.lang.reflect.Method.invoke(Method.java:525) 
10-19 21:50:17.445: E/ActivityThread(24138): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
10-19 21:50:17.445: E/ActivityThread(24138): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-19 21:50:17.445: E/ActivityThread(24138): at dalvik.system.NativeStart.main(Native Method) 

답변

2

여기에 문제가 있습니다.

if (mBound){ 
     unbindService(mConnection); 
    } 

서비스는 결코 발생하지, AO

mBound = true: 

을 bindService 구속하지만, onServiceConnected 결코 호출되어 없습니다. 그렇기 때문에 unbindService이 호출되지 않아 누수가 발생합니다.

관련 문제