简体   繁体   English

Android:从服务中获取结果

[英]Android: get result from a service

I have android service which I need to return a result after it has been started. 我有android服务,启动后需要返回结果。 So: 所以:

1) I start the service 1)我开始服务

2) The service does something 2)服务有所作为

3) The service returns a result 3)服务返回结果

I thought that maybe I can use a singleton class to take the result. 我以为也许可以使用单例类来取得结果。 I created a singleton class in order to make the service save the result in it and then take the result from the service by an activity. 我创建了一个单例类,以使服务将结果保存在其中,然后通过活动从服务中获取结果。 Here there's the code of the class: 这里是该类的代码:

public class Result {

    //SIGLETON DECLARATION

    private static Result mInstance = null;

    public static Result getInstance() { 
        if (mInstance == null) {
            mInstance = new Result();
        }
        return mInstance;
    }

    private Result() {
    }

    //CODE

    Object result;
    boolean resultIsSet = false;

    public void deletePreviousResult() {
        this.result = null;
        resultIsSet = false;
    }

    public void setResult(Object result) {
        Log.w("result", "setResult");
        this.result = result;
        resultIsSet = true;
    }

    public Object getResult() {
        Log.w("result", "getResult");
        while(resultIsSet == false) {}
        return this.result;
    }

Here's the code that I use to get the result: 这是我用来获得结果的代码:

public int getInfo() {
    Result.getInstance().deletePreviousResult();
    //start the service and ask to save the result in singleton class
    Intent myIntent = new Intent(mInstanceContext, MediaPlayerService.class);
    myIntent.setAction("getInfo");
    mInstanceContext.startService(myIntent);
    //take the result
    return (Integer) Result.getInstance().getResult();
}

What is doing in onStartCommand is: onStartCommand是:

 Result.getInstance().setResult(mMediaPlayer.getCurrentPosition()); 

where mMediaPlayer is a MediaPlayer object. 其中, mMediaPlayer是MediaPlayer对象。

However the problem is that if setResult is also supposed to be called in onStartCommand , it is never called! 然而,问题是,如果setResult也应该在被称为onStartCommand ,这是从来没有叫! Why? 为什么? Is it my approach wrong? 我的方法错了吗?

This code you have in getInfo() : 您在getInfo()这段代码:

mInstanceContext.startService(myIntent);
//take the result
return (Integer) Result.getInstance().getResult();

assumes that when you call startService() the service is started and onStartCommand() is called synchronously so that in the next statement you can get the result using Result.getInstance().getResult() . 假设您在调用startService()启动了服务,并且onStartCommand()同步调用,以便在下Result.getInstance().getResult()语句中可以使用Result.getInstance().getResult()获得结果。

Unfortunately, it doesn't work that way. 不幸的是,它不能那样工作。 Calling startService() isn't like calling a method on an object. 调用startService()不同于在对象上调用方法。 What you do when you call startService() is that you tell Android that you want that service to be started at the next available moment . 当您调用startService()时,您所做的就是告诉Android您希望该服务在下一个可用时刻启动。 Since onStartCommand() in your service needs to be called on the main thread, usually this means that Android will start your service and call onStartCommand() at the next time when Android gets control of the main thread (ie: when all your activity methods have returned). 由于您需要在主线程上调用服务中的onStartCommand() ,因此通常这意味着Android将在下次获得Android对主线程的控制时(即:当您所有的活动方法都获得控制时onStartCommand()启动服务并调用onStartCommand() 。已经返回)。 In any case, you can't determine when the service will be started or when onStartCommand() will be called. 无论如何,您无法确定何时启动服务或何时onStartCommand() It is an asynchronous call, so you need to rely on a callback mechanism. 这是一个异步调用,因此您需要依赖于回调机制。 This means that you need to write some kind of callback method that the service can call when it has done the work and wants to return the result to you. 这意味着您需要编写某种回调方法,服务在完成工作并希望将结果返回给您时可以调用该方法。

There are various ways to do this. 有多种方法可以做到这一点。 You could have the service send an Intent to your activity. 您可以让服务将Intent发送给您的活动。 You could have the service broadcast an Intent with the results. 您可以让该服务广播带有结果的Intent。 You could bind to the service and register a callback listener (see developer documentation for bound services). 您可以绑定到该服务并注册一个回调侦听器(请参阅开发人员文档以了解绑定的服务)。 There are probably other ways too. 可能还有其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM