【问题标题】:How to receive values from an IntentService in another IntentService?如何从另一个 IntentService 中的 IntentService 接收值?
【发布时间】:2015-11-17 21:34:38
【问题描述】:

我有这个 IntentService (HttpService),它从 web 服务获取原始 json 数据:

public class HttpService extends IntentService {

    public static final String BROADCAST_ACTION = "com.example.HttpService";

    public HttpService() {
        super("HttpService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        //code to get a String with jsonData


         //Then I send a broadcast
         Intent broadcastHttpResponseIntent = new Intent();
         broadcastHttpResponseIntent.setAction(BROADCAST_ACTION);
         broadcastHttpResponseIntent.putExtra("jsonData", jsonData);

         sendBroadcast(broadcastHttpResponseIntent);

    }
}

现在从使用 HttpService 的 IntentService 我正在尝试获取广播:

public class RestaurantModel extends IntentService {

    public static final String BROADCAST_ACTION = "com.example.RestaurantModel";

    public RestaurantModel() {
        super("RestaurantModel");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v("RestaurantModel", "onHandleIntent");

        BroadcastReceiver httpBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.v("RestaurantModel", "onReceive");

                String jsonResponse = intent.getStringExtra("jsonData");

            }
        };

        Intent getRestaurantsJsonIntent = new Intent(RestaurantModel.this, HttpService.class);
        getRestaurantsJsonIntent.putExtra("urlRestaurants", intent.getStringExtra("urlRestaurants"));
        startService(getRestaurantsJsonIntent);

        registerReceiver(httpBroadcastReceiver, new IntentFilter(HttpService.BROADCAST_ACTION));
    }
}

所以我收到了这个错误:

RestaurantModel 泄露了最初在此处注册的 IntentReceiver com.example.RestaurantModel$1@42374590。您是否错过了对 unregisterReceiver() 的调用?

所以我尝试注销接收器,但似乎需要一个上下文来注销接收器。

如何从一个 IntentService 接收值到另一个 IntentService?

【问题讨论】:

    标签: android android-intent android-activity android-service android-intentservice


    【解决方案1】:

    最好的答案是:只有一个IntentService

    下一个最佳答案是:完全摆脱广播内容,让第一个 IntentService 调用 startService() 来启动第二个 IntentService

    【讨论】:

      【解决方案2】:

      同意@CommonsWare,如果您想在 IntentService 中使用 BroadcaseReceiver,请在 onCreate 方法中注册它并在 onDestroy 方法中取消注册。

      public class RestaurantModel extends IntentService {
      
      public static final String BROADCAST_ACTION = "com.example.RestaurantModel";
      
      private BroadcastReceiver httpBroadcastReceiver;
      
      public RestaurantModel() {
          super("RestaurantModel");
      }
      
      @Override
      public void onCreate() {
          super.onCreate();
           httpBroadcastReceiver = new BroadcastReceiver() {
              @Override
              public void onReceive(Context context, Intent intent) {
                  Log.v("RestaurantModel", "onReceive");
      
                  String jsonResponse = intent.getStringExtra("jsonData");
      
              }
          };
      
          LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(httpBroadcastReceiver);
      }
      
      @Override
      public void onDestroy() {
          super.onDestroy();
          LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(httpBroadcastReceiver);
      }
      
      @Override
      protected void onHandleIntent(Intent intent) {
          Log.v("RestaurantModel", "onHandleIntent");
      
      
          Intent getRestaurantsJsonIntent = new Intent(RestaurantModel.this, HttpService.class);
          getRestaurantsJsonIntent.putExtra("urlRestaurants", intent.getStringExtra("urlRestaurants"));
          startService(getRestaurantsJsonIntent);
      
      
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多