2011-02-01 31 views
0

我想創建一個後臺線程(執行即使我死了的活動),其更新我的數據位置等它有一個http resquest發送到我的服務器。但它發生了異常:無法在AsyncTask中調用處理程序?

02-01 07:50:18.172: ERROR/Exception1(277): Can't create handler inside thread that has not called Looper.prepare() 

我的代碼:

public class Messagerie extends Activity { 
private LocationManager locationManager; 
private String locationProvider = LocationManager.NETWORK_PROVIDER; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.messagerie); 

    new UpdateLocation().execute(); 

} 

public void majCoordonnes() { 
    StringBuilder stringBuilder = new StringBuilder("Fournisseur :"); 
    stringBuilder.append("\n"); 
    stringBuilder.append(locationProvider); 
    stringBuilder.append(" : "); 
    Location location = locationManager.getLastKnownLocation(locationProvider); 

    if (location != null) { 

     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 

     String lat = String.valueOf(latitude); 
     String lon = String.valueOf(longitude); 

     stringBuilder.append(latitude); 
     stringBuilder.append(", "); 
     stringBuilder.append(longitude); 


        //Send location to server in an AsyncTask 
     new sendLocation().execute(lat, lon); 


    } else { 
     stringBuilder.append("Non déterminée"); 
    } 
    Log.d("MaPositionMaj", stringBuilder.toString()); 
} 


/** 
* Ecouteur utilisé pour les mises à jour des coordonnées 
*/ 
class MajListener implements android.location.LocationListener { 
    public void onLocationChanged(Location location) { 
     majCoordonnes(); 
    } 
    public void onProviderDisabled(String provider){ 
    } 
    public void onProviderEnabled(String provider){ 
    } 
    public void onStatusChanged(String provider, int status, Bundle extras){ 
    } 
}; 




//Requête GET, mise à jour des infos 
private class UpdateLocation extends AsyncTask<String, Void, String> { 

    String resp; 

    @Override 
    protected String doInBackground(String... params) { 

     try { 
     //Recuperation Location 
     String locationContext = Context.LOCATION_SERVICE; 
     locationManager = (LocationManager) getSystemService(locationContext); 
     if (locationManager != null && locationProvider != null) { 

      majCoordonnes(); 
      locationManager.requestLocationUpdates(locationProvider, 120000, 500, new MajListener()); 


     } 
     } catch (Exception e){ 
      Log.e("Exception1", e.getMessage()); 
     } 

     return resp; 
    } 


    @Override 
    protected void onPostExecute(String result) { 


    } 


    @Override 
    protected void onPreExecute() { 
    // Things to be done before execution of long running operation. For example showing ProgessDialog 
    } 


    @Override 
    protected void onProgressUpdate(Void... values) { 

    } 
} 

編輯:我想我必須使用一個服務,而不是一個的AsyncTask的。

+0

@ user40574:喜,以更清晰的方式解釋你的問題,你有你的logcat的任何期望或foreround線程無法更新數據????????????????? ????/ – 2011-02-01 08:38:25

+0

我剛剛用logcat更新了我的帖子。 – user420574 2011-02-01 08:40:17

回答

2

編輯:我想我必須使用服務而不是asyncTask。

準確地說。創建單獨的線程並不能確保在應用程序/活動退出時或者操作系統需要更多資源時它不會被終止。 A Service不能確保 - 如果系統處於非常重的負載下,它可能仍然會被殺死,但它的可能性要小得多。此外(糾正我,如果我錯了),Android將重新啓動服務,一旦系統加載軟化。

0

只是調用(它寫在你的代碼)Looper.prepare(),你運行你Thread之前。 (這是一個簡單的解決方案,但以前讓我奇怪了。)

如果您不能調用Handler這是你的Activity,那麼首先,你應該讓你的活動構造,例如:

public SampleActivity() { 
    super(); 
    // TODO Auto-generated constructor stub 
} 

然後在ASyncTask,發起Activity

SampleActivity sampleAct = new SampleActivity(); 

,最終踢是打電話給你Handler

sampleAct.handler.sendMessage(msg); 

我希望這可以清楚地向您解釋。

相關問題