【问题标题】:How to gets parameters from service to activity如何从服务获取参数到活动
【发布时间】:2012-06-27 15:21:09
【问题描述】:

我写了一些代码,活动启动一个服务以从 URL 获取一些文本,在服务完成后我想将此参数返回给活动。

我的代码如下所示:

public class splash extends AsyncTask<String, Void, String> {

  private Context context; 
  private ProgressDialog Dialog; 
  protected config app;

  public splash(Context context){
      this.context=context;
      Dialog = new ProgressDialog(context);
  }

  protected String doInBackground(String... params) {
      //starts service number activite
      Intent serviceIntent = new Intent();
      serviceIntent.setAction("services.conServise");
      context.startService(serviceIntent);

我不想使用全局参数而不是单例

【问题讨论】:

    标签: android design-patterns service


    【解决方案1】:

    如果您想从您的Activity 调用Service,您需要的是bound service。它将您的服务绑定到一个变量,您将能够在Activity 中使用它。

    【讨论】:

      【解决方案2】:

      您需要使用BroadcastReceiver 在活动广告服务之间进行通信。创建自定义广播接收器以将数据从服务发送回活动。请参阅本教程了解我们如何使用自定义广播接收器在服务和活动之间进行通信

      Custom Intents and Broadcasting with Receivers

      Communication between service and activity – Part 1

      【讨论】:

      • 我可以创建一个对象服务然后调用服务类的参数吗?像 MyService.url?还是不好?
      • 或许可行,但正确的方法是使用 BroadcastReceiver 在 Service 和 Activity 之间进行通信
      猜你喜欢
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多