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

查看:2632
本文介绍了如何强制onServiceDisconnected()被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我已经通过调用绑定服务:

After I have bound a service by calling:

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

我需要调试的目的,使onServiceDisconnected()被调用。

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

据我所知,Android系统调用这个当连接到服务意外中断,如当服务崩溃或已被杀害,而当客户端解除绑定这不叫。

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.

所以我的问题是如何强制onServiceDisconnected()被调用时,我想这样我就可以完成测试?

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

推荐答案

您需要启动您的服务,然后绑定Context.BIND_NOT_FOREGROUND标志,然后停止。这会造成onServiceDisconnected被调用。这里是code(假设你有TestService的服务定义)MainActivity与其中两个链接调用doBind和doUnbind方法按钮:

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

}

这code提供了以下日志,当你点击按钮:

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}

这篇关于如何强制onServiceDisconnected()被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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