java – android.app.ServiceConnectionLeaked:Activity … MainActivity泄露了ServiceConnection … MainActivity $1 @ e794142最初绑定在这里

前端之家收集整理的这篇文章主要介绍了java – android.app.ServiceConnectionLeaked:Activity … MainActivity泄露了ServiceConnection … MainActivity $1 @ e794142最初绑定在这里前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
它的失败@下线 –
bindService(intent,m_serviceConnection,Context.BIND_AUTO_CREATE);

以下是追踪….

Activity com.example.alwaysrunningprocesswithcallanswertap.MainActivity has leaked ServiceConnection com.example.alwaysrunningprocesswithcallanswertap.MainActivity$1@e794142 that was originally bound here
android.app.ServiceConnectionLeaked: Activity com.example.alwaysrunningprocesswithcallanswertap.MainActivity has leaked ServiceConnection com.example.alwaysrunningprocesswithcallanswertap.MainActivity$1@e794142 that was originally bound here
    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1077)
    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:971)
    at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1774)
    at android.app.ContextImpl.bindService(ContextImpl.java:1757)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:539)
    at com.example.alwaysrunningprocesswithcallanswertap.MainActivity.onCreate(MainActivity.java:48)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)   
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5257)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

MainActivity.class

public class MainActivity extends Activity 
{
    CallNotifierService m_service;
    boolean isBound = false;

    private ServiceConnection m_serviceConnection = new ServiceConnection() 
    {
        @Override
        public void onServiceConnected(ComponentName className,IBinder service) 
        {
            m_service = ((CallNotifierService.MyBinder)service).getService();
            Toast.makeText(MainActivity.this,"Service Connected",Toast.LENGTH_LONG).show();
            isBound = true;
            Intent intent = new Intent(MainActivity.this,CallNotifierService.class);
            startService(intent);
        }

        @Override
        public void onServiceDisconnected(ComponentName className) 
        {
            Toast.makeText(MainActivity.this,"Service Dis-connected",Toast.LENGTH_LONG).show();
            m_service = null;
            isBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this,CallNotifierService.class);
        bindService(intent,Context.BIND_AUTO_CREATE);
    }

    .
    .
    .

}

解决方法

您需要在代码添加它.
@Override
protected void onDestroy() {
    super.onDestroy();
    unbindService(m_serviceConnection);
    Toast.makeText(MainActivity.this,"Service Un-Binded",Toast.LENGTH_LONG).show();
};
原文链接:https://www.f2er.com/android/126935.html

猜你在找的Android相关文章