Android 如何强制调用onServiceDisconnected()?

Android 如何强制调用onServiceDisconnected()?,android,service,Android,Service,通过调用绑定服务后: bindService(new Intent(IBumpAPI.class.getName()), connection, Context.BIND_AUTO_CREATE); 出于调试目的,我需要调用onServiceDisconnected() 我知道Android系统会在与服务的连接意外丢失时调用此函数,例如当服务崩溃或被终止时,并且在客户端解除绑定时不会调用此函数 因此,我的问题是如何在我需要时强制调用onServiceDisconnected(),以便完成测试?

通过调用绑定服务后:

bindService(new Intent(IBumpAPI.class.getName()), connection, Context.BIND_AUTO_CREATE);
出于调试目的,我需要调用onServiceDisconnected()

我知道Android系统会在与服务的连接意外丢失时调用此函数,例如当服务崩溃或被终止时,并且在客户端解除绑定时不会调用此函数


因此,我的问题是如何在我需要时强制调用onServiceDisconnected(),以便完成测试?

您需要启动服务,然后使用Context.bind\u NOT\u前台标志绑定,然后停止它。这将导致调用onServiceDisconnected。以下是MainActivity的代码(假设您定义了TestService服务),其中有两个按钮链接到调用doBind和doUnbind方法:

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

谢谢,但是unbindService()不调用onServiceDisconnected()。(来自Android文档)我觉得它只有在意外发生时才被调用,而不是由于正常的unbindService()调用。(你是对的,这不是直接代替文件)有什么想法吗?Thanks@DinoZarafonitis对我来说,Context.BIND_不是前台,但你必须调用startService(i);在那之前。我在答案中包含的代码提供了附加的logcat输出。使用Context.BIND\u NOT\u前台时,您可能缺少startService,并且您的服务尚未启动。我应该给您买杯啤酒!:除了调用stopService(i),您还可以打开设置,进入开发者选项->运行服务->应用程序并在那里停止服务。您仍然需要使用Context.bind\u NOT\u前台进行绑定。如果你想在不改变代码的情况下进行测试,这很方便。你是说unbingService和stopService什么都不做吗?要调用onServiceDisconnected,你只需要调用closeServiceIt不调用它,我尝试了。。。我已经用结果更新了问题。可能有点晚了,但这里的解决方案是在停止服务之前不要解除绑定。例如,停止服务,然后解除绑定服务好吧,我不想停止我的服务,只想在我的活动处于后台的时间间隔内解除绑定。我在不调用stopService()的情况下调用unbindService(),并且不调用onServiceDisconnected()。