Android: onReceiveResult 未触发

Android: onReceiveResult is not triggered

我正在尝试从 Service 获取一些数据,该数据从坐标获取地址。我按照有关此 (http://developer.android.com/training/location) 的官方 android 教程一步步进行操作,但我被卡住了。我正在使用 send 方法将数据从 Service class 发送到 Activity,但 onReceiveResult 方法似乎没有触发。这是我的 Service class:

public class FetchAddressIntentService extends IntentService {

    protected ResultReceiver mReceiver;

    public FetchAddressIntentService(String name) {
        super(name);
    }

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

    @Override
    protected void onHandleIntent(Intent intent) {
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        String errorMessage = "";

        // Get the location passed to this service through an extra.
        Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA);
        mReceiver = new ResultReceiver(new Handler());
        List < Address > addresses = null;

        try {
            addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); //we want 1 result
        } catch (IOException ioException) {
            errorMessage = getString(R.string.service_not_available);
            Log.e("TAG", errorMessage, ioException);
        } catch (IllegalArgumentException illegalArgumentException) {
            errorMessage = getString(R.string.invalid_lat_long_used);
            Log.e("TAG", errorMessage + ". " + "Latitude = " + location.getLatitude() + ", Longitude = " + location.getLongitude(), illegalArgumentException);
        }

        if (addresses == null || addresses.size() == 0) {
            if (errorMessage.isEmpty()) {
                errorMessage = getString(R.string.no_address_found);
                Log.e("TAG", errorMessage);
            }
            deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
        } else {
            Address address = addresses.get(0);
            ArrayList < String > addressFragments = new ArrayList < String > ();

            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                addressFragments.add(address.getAddressLine(i));
            }
            Log.i("TAG", getString(R.string.address_found) + ": " + addressFragments.get(0));
            deliverResultToReceiver(Constants.SUCCESS_RESULT, TextUtils.join(System.getProperty("line.separator"), addressFragments));
        }
    }

    private void deliverResultToReceiver(int resultCode, String message) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.RESULT_DATA_KEY, message);
        mReceiver.send(resultCode, bundle);
    }
}

和我的相关部分Activity

class AddressResultReceiver extends ResultReceiver {
    public AddressResultReceiver(Handler handler) {
        super(handler);
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {

        mAddressOutput = resultData.getString(Constants.RESULT_DATA_KEY);
        Log.i("address", mAddressOutput);
        address.setText(mAddressOutput);

        if (resultCode == Constants.SUCCESS_RESULT) {
            Toast.makeText(LocationActivity.this, getString(R.string.address_found),
                    Toast.LENGTH_LONG).show();
        }
    }
}

您需要在 Activity

中创建接收器
private AddressResultReceiver mReceiver;

@Override
protected void onResume() {
    super.onResume();
    mReceiver = new AddressResultReceiver(new Handler());
    mReceiver.setReceiver(this);
}

并传递给 Intent 服务

public void onStarService() {
    final Intent intent = new Intent("SOME_COMMAND_ACTION", null, this, FetchAddressIntentService.class);
    intent.putExtra("RECEIVER", mReceiver);
    startService(intent);
}

并在 FetchAddressIntentService

@Override
protected void onHandleIntent(Intent intent) {
    final ResultReceiver receiver = intent.getParcelableExtra("RECEIVER");
/// your code
}

这一行有问题:

mReceiver = new ResultReceiver(new Handler());

替换为:

mReceiver = intent.getParcelableExtra(Constants.RECEIVER);

问题是您正在创建一个新的 ResultReceiver 而不是使用可能作为 Intent extra 传入的那个 代码:

protected void startIntentService() {
    Intent intent = new Intent(this, FetchAddressIntentService.class);
    intent.putExtra(Constants.RECEIVER, mResultReceiver);
    intent.putExtra(Constants.LOCATION_DATA_EXTRA, mLastLocation);
    startService(intent);
}

所以,只需使用传入的那个作为 Intent 中的 extra,它应该可以工作。