2014-10-31 47 views
0

我試圖找到自己的當前位置project.So我發現這個職位:獲取位置不工作新設備上

What is the simplest and most robust way to get the user's current location on Android?

我嘗試使用locationclient但不滿意結果我已經使用.The代碼如下:

的Mylocation.java類:

package com.igloo.classes; 

import java.util.Timer; 
import java.util.TimerTask; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

public class MyLocation { 
    Timer timer1; 
    LocationManager lm; 
    LocationResult locationResult; 
    boolean gps_enabled=false; 
    boolean network_enabled=false; 


    public boolean getLocation(Context context, LocationResult result) 
    { 
     //I use LocationResult callback class to pass location value from MyLocation to user code. 
     locationResult=result; 
     if(lm==null) 
      lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

     //exceptions will be thrown if provider is not permitted. 
     try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){} 
     try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){} 

     //don't start listeners if no provider is enabled 
     if(!gps_enabled && !network_enabled) 
      return false; 

     if(gps_enabled) 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); 
     if(network_enabled) 
      lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); 
     timer1=new Timer(); 
     timer1.schedule(new GetLastLocation(), 90000); 
     return true; 
    } 

    LocationListener locationListenerGps = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      timer1.cancel(); 
      locationResult.gotLocation(location); 
      lm.removeUpdates(this); 
      lm.removeUpdates(locationListenerNetwork); 
     } 
     public void onProviderDisabled(String provider) {} 
     public void onProviderEnabled(String provider) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    }; 

    LocationListener locationListenerNetwork = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      timer1.cancel(); 
      locationResult.gotLocation(location); 
      lm.removeUpdates(this); 
      lm.removeUpdates(locationListenerGps); 
     } 
     public void onProviderDisabled(String provider) {} 
     public void onProviderEnabled(String provider) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    }; 

    class GetLastLocation extends TimerTask { 
     @Override 
     public void run() { 
      lm.removeUpdates(locationListenerGps); 
      lm.removeUpdates(locationListenerNetwork); 

      Location net_loc=null, gps_loc=null; 
      if(gps_enabled) 
       gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      if(network_enabled) 
       net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

      //if there are both values use the latest one 
      if(gps_loc!=null && net_loc!=null){ 
       if(gps_loc.getTime()>net_loc.getTime()) 
        locationResult.gotLocation(gps_loc); 
       else 
        locationResult.gotLocation(net_loc); 
       return; 
      } 

      if(gps_loc!=null){ 
       locationResult.gotLocation(gps_loc); 
       return; 
      } 
      if(net_loc!=null){ 
       locationResult.gotLocation(net_loc); 
       return; 
      } 
      locationResult.gotLocation(null); 
     } 
    } 
    public void cancelTimer() 
    { 
     if(timer1!=null) 
     { 
     timer1.cancel(); 
     lm.removeUpdates(locationListenerGps); 
     lm.removeUpdates(locationListenerNetwork); 
     } 
    } 


    public static abstract class LocationResult{ 
     public abstract void gotLocation(Location location); 
    } 
} 

在我的主要類的代碼:我使用了一個回調方法來獲取位置對象。

locationResult = new LocationResult(){ 
      @Override 
      public void gotLocation(Location location){ 
       if(location!=null) 
       { 
       current_latitude=location.getLatitude(); 
       current_longitude=location.getLongitude(); 
       } 
       if(location!=null&&!isLocationRecieved) 
       { 
        MarinaJobKeys.user_viewing_jobs=true; 
        isLocationRecieved=true; 

        Handler handler = new Handler(Looper.getMainLooper()); 
        handler.post(new Runnable(){ 

         @Override 
         public void run() { 
          setcurrentlocation(); 

         } 

        }); 


       } 
       else if(location==null) 
       { 
        Toast.makeText(getActivity(),"NULL LOCATION RETURNED",Toast.LENGTH_LONG).show(); 
       } 

      } 

     }; 





myLocation = new MyLocation(); 
      myLocation.getLocation(getActivity(), locationResult); 

的問題是這樣的:

代碼工作在一個較舊的device.But很好,當我在新設備上運行,在Mylocation類的onLocationchanged方法永遠不會called.But在舊設備它會立即被調用。我該如何解決這個問題?

編輯:問題得到修復重新啓動設備。但爲什麼會發生?

+0

你是什麼意思的老年人和新設備 – 2014-10-31 06:11:49

+0

通過舊的設備我的意思是我的手機的Android 2.6.By較新的設備我的意思是安卓4.0 + .The的事情是,當我的新設備onlocationchanged從來沒有在調試被調用。 – 2014-10-31 06:13:00

+0

您收到任何錯誤? – Amy 2014-10-31 06:17:58

回答

0

您必須啓用位置設置。

請檢查this link

go to setting --> Location Access --> turn on Location. 
+0

這兩個提供商都on.I檢查它們作爲代碼中的布爾變量,並都返回true。 – 2014-10-31 06:16:56

相關問題