【问题标题】:How to delay method call until another method been done first如何延迟方法调用,直到先完成另一个方法
【发布时间】:2017-08-08 18:26:59
【问题描述】:

我正在使用 loader 从 Internet 获取数据并使用 GoogleApiClient 来获取用户位置,我将 带有位置的字符串 url 传递给加载器构造函数,onCreateLoader 方法在 onLocationChanged 方法之前调用,将用户位置作为对象

问题是我需要 onLocationChanged 方法首先返回位置,以便能够将正确的 url 传递给 onCreateLoader 方法。那么,有什么方法可以先运行 onLocationChanged 方法或延迟加载器的创建方法

onCreateLoader 代码

    @Override
public android.content.Loader<Adhan> onCreateLoader(int id, Bundle args) {

    Log.v(LOG_TAG, "onCreateLoader invoked query here " + mQueryUrl);
    if (mQueryUrl != null) {
        return new PrayTimeLoader(getContext(), mQueryUrl);
    }
    return null;
}

onLocationChanged 代码

 @Override
public void onLocationChanged(Location location) {

    mLocation = location;

    makeQueryUrl();


    Log.v(LOG_TAG, "latitude is " + location.getLatitude());
    Log.v(LOG_TAG, "Longitude is " + location.getLongitude());

}

ma​​keQueryUrl 代码

private String makeQueryUrl() {

    StringBuilder theQuery = new StringBuilder();

    Long tsLong = System.currentTimeMillis() / 1000;
    String ts = tsLong.toString();

    if (mLocation != null) {


        String latitude = Double.toString(mLocation.getLatitude());
        String longitude = Double.toString(mLocation.getLongitude());

        theQuery.append("http://api.aladhan.com/timings/")
                .append(ts)
                .append("?latitude=")
                .append(latitude)
                .append("&longitude=")
                .append(longitude)
        ;

        Log.v(LOG_TAG, "our url for today is " + theQuery);
    }
    mQueryUrl = String.valueOf(theQuery);
    return mQueryUrl;
}

日志输出

  • 08-08 20:48:19.406 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment: onCreateLoader 在此处调用查询 null

    08-08 20:48:19.406 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment: onCreateLoader 在此处调用查询 null

    08-08 20:48:19.634 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment: onConnected 调用 true

    08-08 20:48:19.645 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment: 请求位置

    08-08 20:48:19.652 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment:纬度为 30.8567497

    08-08 20:48:19.652 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment:经度为 30.8001775

【问题讨论】:

  • 为什么要先调用 onCreateLoader 才能传递它需要的数据(位置)?
  • 同一个活动实现了 LocationListenerLoaderManager.LoaderCallbacks 所以 onCreateLoader 方法和 onLocationChanged b> 在同一个活动中被覆盖,当我使用日志时,我发现在 onLocationChanged 之前调用了 onCreateLoader 方法。
  • 你在哪里初始化加载器?初始化和启动它的触发器应该在 onLocationChanged 之后。以前没有理由这样做。
  • 确实你是对的谢谢:)

标签: android android-loadermanager asynctaskloader


【解决方案1】:

为什么不直接从 onLocationChanged 内部调用加载程序?

void onLocationChanged(Location location) {
    if (location != null) {
        getLoaderManager().initLoader(0, null, this);
    }
}

或者你可以运行

getLoaderManager().restartLoader(0, null, this);

onLocationChanged() 内部

【讨论】:

  • 加载器扩展了 AsyncTaskLoader ,并且要启动它必须实现 LoaderManager.LoaderCallbacks 并覆盖方法 onCreateLoader 那么如何从另一个方法调用被覆盖的方法?
  • 欢迎发布一些代码,以便我们更好地帮助您。
  • 顺便说一句,您不必让活动实现它。匿名上课也可以。
【解决方案2】:

听起来您正在使用 LoaderManager。我不确定你在哪里初始化它,但尝试在捕获位置后重新初始化加载器:

@Override
public void onLocationChanged(Location location) {

    mLocation = location;

    makeQueryUrl();

    Log.v(LOG_TAG, "latitude is " + location.getLatitude());
    Log.v(LOG_TAG, "Longitude is " + location.getLongitude());

    getLoaderManager().restartLoader(0, null, this);

}

【讨论】:

  • 问题是 initloader 放置并且它有效,不幸的是可以接受一个答案,但谢谢
猜你喜欢
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
  • 2021-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多