安卓:从得到的服务结果 [英] Android: get result from a service

查看:150
本文介绍了安卓:从得到的服务结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开一个新的问题更清晰:)
我有Android的服务,我需要它开始后返回结果。
所以:
1)我启动服务
2)服务做一些事情
3)服务返回结果
我想,也许我可以使用单个类取结果。所以我为了让服务保存结果,并比活动拍摄效果从服务创建一个单独的类。这里有阶级的code:

I open a new question to be more clear :) I have android service which I need to return a result after it has been started. So: 1) I start the service 2) the service do something 3) the service return a result I thought that maybe I can use a singleton class to take the result. So I create a singleton class in order to make the service save the result in it and than 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;
}

和这里有,我用得到的结果code:

And here there'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();
}

在什么是onStartCommand做的是

What is doing in onStartCommand is

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

其中mMediaPlayer是一个MediaPlayer对象。

where mMediaPlayer is a MediaPlayer object.

然而,问题是,也如的setResult假定在onStartCommand被调用,它不会被调用!为什么?是不是我的方法不对?
谢谢你在前进!

However the problem is that, also if setResult is supposed to be called in the onStartCommand, it is never called! Why? Is it my approach wrong? Thank you in advance!

推荐答案

这code你在的getInfo()

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

假设当你调用 startService()服务已启动和 onStartCommand()被称为同步,这样在接下来的语句中,你可以得到使用 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()未比如调用对象的方法。当你叫你做什么 startService()是你告诉Android的,你想要的服务,在下一个可用时刻启动。由于 onStartCommand()需要在服务的主线程上被调用,通常这意味着Android将启动您的服务和呼叫 onStartCommand()在Android的时候得到主线程的控制下一次(即:当你所有的活动方法已经返回)。在任何情况下,你不能或确定何时该服务将被启动时, onStartCommand()将被调用。这是一个同步拨打,所以你需要依靠的回调机制。这意味着你需要写一些回调方法,该服务可以调用时,它所做的工作,并希望将结果返回给你。

Unfortunately, it doesn't work that way. Calling startService() isn't like calling a method on an object. 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. 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). In any case, you can't determine when the service will be started or when onStartCommand() will be called. 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. You could have the service broadcast an Intent with the results. You could bind to the service and register a callback listener (see developer documentation for bound services). There are probably other ways too.

这篇关于安卓:从得到的服务结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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