2015-09-01 66 views
-1

我正在製作一個Android應用程序,我試圖使用位置管理器獲取位置,然後將該位置推送到一臺服務器。當我嘗試這樣做時,我收到以下錯誤。Android:java.lang.RuntimeException:無法在未調用Looper.prepare()的線程中創建處理程序

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
      at android.os.Handler.<init>(Handler.java:200) 
      at android.os.Handler.<init>(Handler.java:114) 
      at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:223) 
      at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:223) 
      at android.location.LocationManager.wrapListener(LocationManager.java:851) 
      at android.location.LocationManager.requestLocationUpdates(LocationManager.java:864) 
      at android.location.LocationManager.requestLocationUpdates(LocationManager.java:459) 
      at com.pickingo.fe.services.gps.AppLocationService.getLocation(AppLocationService.java:30) 
      at com.pickingo.fe.gps.CurrentLocationPusher.run(CurrentLocationPusher.java:60) 
      at java.lang.Thread.run(Thread.java:818) 

這是我appLocationService

public class AppLocationService extends Service implements LocationListener { 

    protected LocationManager locationManager; 
    Location location; 

    private static final long MIN_DISTANCE_FOR_UPDATE = 50; 
    private static final long MIN_TIME_FOR_UPDATE = 1000*60*2; 

    public AppLocationService(Context context) { 
     locationManager = (LocationManager) context 
       .getSystemService(LOCATION_SERVICE); 
    } 

    public Location getLocation(String provider) { 
     if (locationManager.isProviderEnabled(provider)) { 
      locationManager.requestLocationUpdates(provider, 
        MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this); 
      if (locationManager != null) { 
       location = locationManager.getLastKnownLocation(provider); 
       return location; 
      } 
     } 
     return null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

} 

這是我使用從獲得的LocationManager的位置後推位置的代碼。

public class CurrentLocationPusher implements Runnable { 


    private static CurrentLocationPusher _singleInstance; 

    private CurrentLocationPusher() { 
     this.apiClient = ApiClient.getInstance(); 
     this.appLocationService = new AppLocationService(appContext); 
     this.runningThread = new Thread(this); 
    } 

    public static CurrentLocationPusher getInstance() { 
     if (_singleInstance == null) 
      synchronized (CurrentLocationPusher.class) { 
       if (_singleInstance == null) 
        _singleInstance = new CurrentLocationPusher(); 
      } 
     return _singleInstance; 
    } 

    private boolean isStopThread = false; 

    public static final long ONE_MINUTE = 60 * 1000; 

    private static Context appContext; 

    private AppLocationService appLocationService; 

    private ApiClient apiClient; 
    private Thread runningThread; 


    public static void init(Context applicationContext) { 

     appContext = applicationContext; 
    } 

    @Override 
    public void run() { 

     Location location = appLocationService 
       .getLocation(LocationManager.GPS_PROVIDER); 
     while (!isStopThread) { 
      try { 
       if (location != null) 
        if (CommonUtils.isActiveToInternet(appContext)) 
         apiClient.getApi(appContext).pushLocation(String.valueOf(location.getLatitude()), 
           String.valueOf(location.getLongitude()),String.valueOf(System.currentTimeMillis()), 
           String.valueOf(location.getAccuracy())); 
       Thread.sleep(ONE_MINUTE); 
      } catch (InterruptedException e) { 

      } catch (Exception e) { 
       Log.e(PickingoConstant.TAG, "error" + e.getMessage()); 
      } 
     } 

    } 


    public void startPushingLocation() { 
     if (this.runningThread.getState() == Thread.State.NEW) { 
      runningThread.start(); 
      isStopThread = false; 
     } else if (this.runningThread.getState() == Thread.State.TERMINATED) { 
      (runningThread = new Thread(this)).start(); 
      isStopThread = false; 
     } else if (this.runningThread.getState() == Thread.State.RUNNABLE) { 
      return; 
     } 

    } 

    public void stopPushingLocation() { 
     isStopThread = true; 
    } 
} 

我在CurrentLocationPusher

  .getLocation(LocationManager.GPS_PROVIDER); 

得到錯誤在以下行如果有人能給出提示或者一些幫助,我真的很感激。提前致謝 !!

+1

你不需要所有這些。看看@ https://developer.android.com/training/location/receive-location-updates.html。示例@ github https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates – Raghunandan

+0

什麼是pushLocation(); –

+0

它使用任何烤麪包或任何ui更新元素? –

回答

1

正如Michael在run()方法開始時添加此代碼所指出的那樣。

if (Looper.myLooper() == null) { 
      Looper.prepare(); 
     } 
相關問題