如何强制调用onServiceDisconnected()?[英] How to force the onServiceDisconnected() get called?

本文是小编为大家收集整理的关于如何强制调用onServiceDisconnected()?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

通过呼叫绑定服务:

bindService(new Intent(IBumpAPI.class.getName()), connection, Context.BIND_AUTO_CREATE);

我需要调试目的,以使onserviceCisconnected()称为.

我知道,当与服务的连接出乎意料地丢失时,android系统调用此问题,例如当服务崩溃或已被杀死时,当客户端取消绑定时,不会调用这一点.

所以我的问题是如何强制onserviceConnected()在我想要的时候得到的调用,所以我可以完成测试?

推荐答案

您需要启动您的服务,然后与context.bind_not_foreground标志绑定,然后停止它.这将导致调用OnServiceConceConnected.以下是具有与调用Dobind和Dounbind方法相关的两个按钮的rainactivity的代码(假设您有测试定义):

package com.example.servicetest;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";

    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "Service disconnected: " + name);
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "Service connected: " + name);
        }
    };

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

    }

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

    public void doBind(View v) {
        Intent i = new Intent(this, TestService.class);
        startService(i);
        bindService(i, connection, Context.BIND_NOT_FOREGROUND);
    }

    public void doUnbind(View v) {
        Intent i = new Intent(this, TestService.class);
        stopService(i);
    }

}

此代码在单击按钮时提供以下日志:

11-27 09:21:57.326: D/MainActivity(10724): Service connected: ComponentInfo{com.example.servicetest/com.example.servicetest.TestService}
11-27 09:21:58.099: D/MainActivity(10724): Service disconnected: ComponentInfo{com.example.servicetest/com.example.servicetest.TestService}

其他推荐答案

您可能只是释放您的服务

public void openService {
mConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mService = IService.Stub.asInterface(service);
            }
            @Override
            public void onServiceDisconnected(ComponentName cn) {
                mService = null;
            }
        };
         bindService(service, mConnection, Context.BIND_AUTO_CREATE);   
    }
}

public void closeService() {
    unbindService(mConnection);
    stopService(new Intent(this, Service.class));
}

本文地址:https://www.itbaoku.cn/post/102610.html

问题描述

After I have bound a service by calling:

bindService(new Intent(IBumpAPI.class.getName()), connection, Context.BIND_AUTO_CREATE);

I need for debugging purposes to make the onServiceDisconnected() get called.

I am aware that the Android system calls this when the connection to the service is unexpectedly lost, such as when the service has crashed or has been killed and that this is not called when the client unbinds.

So my question is how to force the onServiceDisconnected() get called whenever I want so I can complete a test?

推荐答案

You need to start you service then bind with Context.BIND_NOT_FOREGROUND flag and then stop it. This will cause onServiceDisconnected to be called. Here is code (assuming that you have TestService service defined) of MainActivity with two buttons which are linked to call doBind and doUnbind methods:

package com.example.servicetest;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";

    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "Service disconnected: " + name);
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "Service connected: " + name);
        }
    };

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

    }

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

    public void doBind(View v) {
        Intent i = new Intent(this, TestService.class);
        startService(i);
        bindService(i, connection, Context.BIND_NOT_FOREGROUND);
    }

    public void doUnbind(View v) {
        Intent i = new Intent(this, TestService.class);
        stopService(i);
    }

}

This code provides following logs when you click on buttons:

11-27 09:21:57.326: D/MainActivity(10724): Service connected: ComponentInfo{com.example.servicetest/com.example.servicetest.TestService}
11-27 09:21:58.099: D/MainActivity(10724): Service disconnected: ComponentInfo{com.example.servicetest/com.example.servicetest.TestService}

其他推荐答案

You might just unbind your service

public void openService {
mConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mService = IService.Stub.asInterface(service);
            }
            @Override
            public void onServiceDisconnected(ComponentName cn) {
                mService = null;
            }
        };
         bindService(service, mConnection, Context.BIND_AUTO_CREATE);   
    }
}

public void closeService() {
    unbindService(mConnection);
    stopService(new Intent(this, Service.class));
}