不使用时,BIND_AUTO_CREATE泄漏ServiceConnection [英] ServiceConnection leak when not using BIND_AUTO_CREATE

查看:3021
本文介绍了不使用时,BIND_AUTO_CREATE泄漏ServiceConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您给我解释一下,当我们绑定到业务会发生什么,但从来没有启动它,然后解除绑定?我得到活动已经泄露服务连接错误,但我不明白为什么。

为MyService:
    包com.example.servicetest;

 进口android.app.Service;
进口android.content.Intent;
进口android.os.Binder;
进口android.os.IBinder;公共类为MyService扩展服务{    活页夹mLocalBinder =新活页夹();    @覆盖
    公众的IBinder onBind(意向意图){        返回mLocalBinder;
    }}

MainActivity:

 包com.example.servicetest;进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Intent;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        按钮按钮1 =(按钮)findViewById(R.id.button1);
        button1.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                意向意图=新意图(MainActivity.this,BoundActivity.class);
                startActivity(意向);            }
        });
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);        返回true;
    }}

BoundActivity:

 包com.example.servicetest;进口android.os.Bundle;
进口android.os.IBinder;
进口android.app.Activity;
进口android.content.ComponentName;
进口android.content.Intent;
进口android.content.ServiceConnection;
进口android.view.Menu;公共类BoundActivity延伸活动{布尔mBound = FALSE;
ServiceConnection mConnection =新ServiceConnection(){    @覆盖
    公共无效onServiceDisconnected(组件名名){
        mBound = FALSE;    }    @覆盖
    公共无效onServiceConnected(组件名名称,服务的IBinder){
       mBound = TRUE;
    }
};
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_bound);
}@覆盖
保护无效onResume(){
    super.onResume();
    意向书的服务=新意图(这一点,MyService.class);
    bindService(服务,mConnection,0);}@覆盖保护无效的onPause(){
    如果(mBound){
        unbindService(mConnection);
    }
    super.onPause();
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.bound,菜单);
    返回true;
}

}

当我点击Button1的话,BoundActivity启动并将其绑定到的MyService。当我点击后,我得到以下异常:

  21 10-19:50:17.445:E / ActivityThread(24138):活动com.example.servicetest.BoundActivity渗漏ServiceConnection com.example.servicetest.BoundActivity$1@41deb340了这里原本是必然
10-19 21:50:17.445:E / ActivityThread(24138):android.app.ServiceConnectionLeaked:活动com.example.servicetest.BoundActivity渗漏,最初这里必然ServiceConnection com.example.servicetest.BoundActivity$1@41deb340
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.LoadedApk $ ServiceDispatcher<&初始化GT;(LoadedApk.java:974)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.ContextImpl.bindServiceAsUser(ContextImpl.java:1452)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.ContextImpl.bindService(ContextImpl.java:1440)
10-19 21:50:17.445:E / ActivityThread(24138):在android.content.ContextWrapper.bindService(ContextWrapper.java:496)
10-19 21:50:17.445:E / ActivityThread(24138):在com.example.servicetest.BoundActivity.onResume(BoundActivity.java:37)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.Activity.performResume(Activity.java:5211)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)
10-19 21:50:17.445:E / ActivityThread(24138):在android.os.Handler.dispatchMessage(Handler.java:99)
10-19 21:50:17.445:E / ActivityThread(24138):在android.os.Looper.loop(Looper.java:137)
10-19 21:50:17.445:E / ActivityThread(24138):在android.app.ActivityThread.main(ActivityThread.java:5103)
10-19 21:50:17.445:E / ActivityThread(24138):在java.lang.reflect.Method.invokeNative(本机方法)
10-19 21:50:17.445:E / ActivityThread(24138):在java.lang.reflect.Method.invoke(Method.java:525)
10-19 21:50:17.445:E / ActivityThread(24138):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
10-19 21:50:17.445:E / ActivityThread(24138):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-19 21:50:17.445:E / ActivityThread(24138):在dalvik.system.NativeStart.main(本机方法)


解决方案

问题在于这里。

 如果(mBound){
        unbindService(mConnection);
    }

服务由bindService约束,但 onServiceConnected 从未卡列斯,AO

  mBound =真:

永远不会发生。这就是为什么 unbindService 不叫,导致泄漏。

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)

解决方案

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.

这篇关于不使用时,BIND_AUTO_CREATE泄漏ServiceConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆