ServiceConnection leak when not using BIND_AUTO_CREATE

不羁的心 提交于 2019-12-11 04:40:06

问题


Could you please explain to me, what happens when we bind to service, but never start it and then unbind? I'm getting "Activity has leaked a Service Connection error", but I do not understand why.

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

}

When I click then button1, BoundActivity is started and it binds to MyService. When I click back, I get following exception:

10-19 21:50:17.445: E/ActivityThread(24138): Activity com.example.servicetest.BoundActivity has leaked ServiceConnection com.example.servicetest.BoundActivity$1@41deb340 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 com.example.servicetest.BoundActivity$1@41deb340 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)

回答1:


Problem resides here.

if (mBound){
        unbindService(mConnection);
    }

Service is bound by bindService, but onServiceConnected never calles, ao

mBound = true:

never occurs. That's why unbindService is not called, resulting in a leak.



来源:https://stackoverflow.com/questions/19470285/serviceconnection-leak-when-not-using-bind-auto-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!