2017-08-08 32 views
0

我使用裝載機從互聯網和GoogleApiClient取數據,以獲取用戶的位置我通過與位置一個字符串的URL裝載機構造,所謂onCreateLoader方法onLocationChanged方法獲取用戶的位置作爲對象如何延遲方法調用,直到另一種方法已經完成第一

以前的問題是我需要onLocationChanged方法先返回的位置,就能夠通過正確的網址爲t他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()); 

} 

makeQueryUrl代碼

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調用查詢這裏空

    08-08 20:48:19.406 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment:onCreateLoader在這裏調用查詢null

    08-08 20:4 8:19.634 25518-25518/com.example.worldwide.wzakerapp V/PrayerFragment:onConnected調用真實

    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

+0

爲什麼你會onCreateLoader叫你可以通過它需要的數據(位置)之前? –

+0

相同的活動實現** LocationListener **和** LoaderManager.LoaderCallbacks **,所以** onCreateLoader **方法和** onLocationChanged **在同一個活動中被覆蓋,當我使用日誌計算出之前調用的onCreateLoader方法onLocationChanged。 – MuhammadAliJr

+0

你究竟在哪裏初始化加載器?初始化並啓動它的觸發器應該在onLocationChanged之後。沒有理由這樣做。 –

回答

1

你爲什麼不從onLocationChanged裏面調用loader呢?

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

或者你可以運行

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

onLocationChanged的()內

+0

加載器擴展AsyncTaskLoader,並啓動它必須實現LoaderManager.LoaderCallbacks並覆蓋方法onCreateLoader 那麼如何從另一個方法調用重寫的方法? – MuhammadAliJr

+0

隨時發佈一些你的代碼,所以我們可以更好地幫助你。 – anomeric

+0

完成添加代碼我認爲它的相關 – MuhammadAliJr

0

這聽起來像你正在使用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); 

} 
+0

問題是initloader的安置和它的工作,不幸的是一個答案可以接受,但謝謝你 – MuhammadAliJr

相關問題