2014-09-01 96 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卡的设备上非常有魅力。