2014-09-01 95 views
0

我跟隨下面的鏈接來檢測我的程序試圖檢測我的應用程序

How to get Latitude and Longitude of the mobile device in android?設備的位置的Android設備的位置入門「源未找到」錯誤。

但我的應用程序不斷給「源找不到」運行時異常。任何人都可以解釋它背後的原因。我也發佈我的代碼如下: -

public void getLocation() { LocationManager locationmgr =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

Location location = locationmgr.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

LocationListener locationListener = new LocationListener() { 

    public void onLocationChanged(Location location) { 
     location_longitude = location.getLongitude(); 
     location_latitude = location.getLatitude(); 
    } 

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

    public void onProviderEnabled(String provider) {} 

    public void onProviderDisabled(String provider) {} 

}; 

locationmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener); 
+0

張貼錯誤日誌。您是否添加了訪問權限的好位置權限 – 2014-09-01 07:40:31

+0

是的,我已經添加了權限。實際上我已經添加了這兩個權限。實際上它不是一個錯誤,而是一個運行時異常,其中顯示「未找到源」@RachitaNanda – zstart 2014-09-01 07:56:55

回答

2

我用這個類併成功找到了用戶的當前位置。希望它會爲你工作?

public class LocationTracker extends Service implements LocationListener { 

private final Context mContext; 

// flag for GPS status 
boolean isGPSEnabled = false; 

// flag for network status 
boolean isNetworkEnabled = false; 

// flag for GPS status 
boolean canGetLocation = false; 

Location location; // location 
double latitude; // latitude 
double longitude; // longitude 


private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

// The minimum time between updates in milliseconds 
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

// Declaring a Location Manager 
protected LocationManager locationManager; 

public LocationTracker(Context context){ 
    this.mContext = context; 
    getLocation(); 
} 

public Location getLocation(){ 

    try { 
     locationManager = (LocationManager) mContext 
       .getSystemService(LOCATION_SERVICE); 

     // getting GPS status 
     isGPSEnabled = locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 

     // getting network status 
     isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 
     } else { 
      this.canGetLocation = true; 
      // First get location from Network Provider 
      if (isNetworkEnabled) { 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       Log.d("Network", "Network"); 
       if (locationManager != null) { 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 
      } 
      if (isGPSEnabled) { 
       if (location == null) { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("GPS Enabled", "GPS Enabled"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return location; 

} 

/** 
* Stop using GPS listener 
* Calling this function will stop using GPS in your app 
* */ 
public void stopUsingGPS(){ 
    if(locationManager != null){ 
     locationManager.removeUpdates(LocationTracker.this); 
    }  
} 

/** 
* Function to get latitude 
* */ 
public double getLatitude(){ 
    if(location != null){ 
     latitude = location.getLatitude(); 
     Log.d("lat", latitude+""); 
    } 

    // return latitude 
    return latitude; 
} 

/** 
* Function to get longitude 
* */ 
public double getLongitude(){ 
    if(location != null){ 
     longitude = location.getLongitude(); 
     Log.d("long :", longitude+""); 
    } 

    // return longitude 
    return longitude; 
} 


/** 
* Function to check GPS/wifi enabled 
* @return boolean 
* */ 
public boolean canGetLocation() { 
    return this.canGetLocation; 
} 

/** 
* Function to show settings alert dialog 
* On pressing Settings button will lauch Settings Options 
* */ 
public void showSettingsAlert(){ 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

    // Setting Dialog Title 
    alertDialog.setTitle("GPS settings"); 

    // Setting Dialog Message 
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

    // On pressing Settings button 
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog,int which) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      mContext.startActivity(intent); 
     } 
    }); 

    // on pressing cancel button 
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     dialog.cancel(); 
     } 
    }); 

    // Showing Alert Message 
    alertDialog.show(); 
} 

@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; 
} 

}

,並在您的活動創建它的對象並獲取類似如下的位置....

LocationTracker locationTracker = new LocationTracker(StreamActivity.this); 
if (!locationTracker.canGetLocation()) { 
      locationTracker.showSettingsAlert(); 
     } else { 

      latitude = locationTracker.getLatitude; 
      longitude = locationTracker.getLongitude; 


      locationTracker.stopUsingGPS(); 

     } 

,並添加所有在清單文件的權限....

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

好運....

+0

它一直拋出此源未發現異常。早些時候它在執行「requestlocationupdates()」時拋出了這個異常,現在在實現了您提供的代碼之後,我在執行「isProviderEnabled()」時得到了相同的異常。這背後的原因是什麼?我錯過了一些圖書館或東西? @Sachin – zstart 2014-09-01 10:21:57

+0

你有從你的設備啓用位置服務? – Sachin 2014-09-01 11:07:59

+0

你也可以通過這個鏈接[http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/] @zstart – Sachin 2014-09-01 11:11:08

0

問題是我在沒有SIM卡的手機上測試此代碼。因爲我使用「GPS提供商」,所以我認爲沒有必要在設備上插入SIM卡。但是我錯了。它在插入SIM卡的設備上非常有魅力。