2016-12-09 8 views
1

У меня возникли проблемы с решить текущую проблему определения местоположения, где LocationManager дает ошибку этого:Как решить вызов Требуется разрешение [Android]

Error message

Я добавил этот код, но и его потребности return value. Должен ли я просто вернуть null или ...?

Ниже мой код для размещения:

public class GPSService extends Service implements LocationListener { 

// saving the context for later use 
private final Context mContext; 

// if GPS is enabled 
boolean isGPSEnabled = false; 
// if Network is enabled 
boolean isNetworkEnabled = false; 
// if Location co-ordinates are available using GPS or Network 
public boolean isLocationAvailable = false; 

// Location and co-ordinates coordinates 
Location mLocation; 
double mLatitude; 
double mLongitude; 

// Minimum time fluctuation for next update (in milliseconds) 
private static final long TIME = 30000; 
// Minimum distance fluctuation for next update (in meters) 
private static final long DISTANCE = 20; 

// Declaring a Location Manager 
protected LocationManager mLocationManager; 

public GPSService(Context context) { 
    this.mContext = context; 
    mLocationManager = (LocationManager) mContext 
      .getSystemService(LOCATION_SERVICE); 
} 

/** 
* Returs the Location 
* 
* @return Location or null if no location is found 
*/ 
public Location getLocation() { 
    try { 

     // If reaching here means, we were not able to get location neither 
     // from GPS not Network, 
     if (!isGPSEnabled) { 
      // so asking user to open GPS 
      askUserToOpenGPS(); 
     } 

     mLocationManager = (LocationManager) mContext 
       .getSystemService(LOCATION_SERVICE); 

     // Getting GPS status 
     isGPSEnabled = mLocationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 

if (Build.VERSION.SDK_INT >= 23 && 
       ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
       ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return ; 


     // If GPS enabled, get latitude/longitude using GPS Services 
     if (isGPSEnabled) { 
      mLocationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, TIME, DISTANCE, this); 
      if (mLocationManager != null) { 
       mLocation = mLocationManager 
         .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
       if (mLocation != null) { 
        mLatitude = mLocation.getLatitude(); 
        mLongitude = mLocation.getLongitude(); 
        isLocationAvailable = true; // setting a flag that 
        // location is available 
        return mLocation; 
       } 
      } 
     } 

     // If we are reaching this part, it means GPS was not able to fetch 
     // any location 
     // Getting network status 
     isNetworkEnabled = mLocationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (isNetworkEnabled) { 
      mLocationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, TIME, DISTANCE, this); 
      if (mLocationManager != null) { 
       mLocation = mLocationManager 
         .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
       if (mLocation != null) { 
        mLatitude = mLocation.getLatitude(); 
        mLongitude = mLocation.getLongitude(); 
        isLocationAvailable = true; // setting a flag that 
        // location is available 
        return mLocation; 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    // if reaching here means, location was not available, so setting the 
    // flag as false 
    isLocationAvailable = false; 
    return null; 
    } 
} 

/** 
* Gives you complete address of the location 
* 
* @return complete address in String 
*/ 
public String getLocationAddress() { 

    if (isLocationAvailable) { 

     Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); 
     // Get the current location from the input parameter list 
     // Create a list to contain the result address 
     List<Address> addresses = null; 
     try { 
      /* 
      * Return 1 address. 
      */ 
      addresses = geocoder.getFromLocation(mLatitude, mLongitude, 1); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
      return ("IO Exception trying to get address:" + e1); 
     } catch (IllegalArgumentException e2) { 
      // Error message to post in the log 
      String errorString = "Illegal arguments " 
        + Double.toString(mLatitude) + " , " 
        + Double.toString(mLongitude) 
        + " passed to address service"; 
      e2.printStackTrace(); 
      return errorString; 
     } 
     // If the reverse geocode returned an address 
     if (addresses != null && addresses.size() > 0) { 
      // Get the first address 
      Address address = addresses.get(0); 
      /* 
      * Format the first line of address (if available), city, and 
      * country name. 
      */ 
      String addressText = String.format(
        "%s, %s, %s", 
        // If there's a street address, add it 
        address.getMaxAddressLineIndex() > 0 ? address 
          .getAddressLine(0) : "", 
        // Locality is usually a city 
        address.getLocality(), 
        // The country of the address 
        address.getCountryName()); 
      // Return the text 
      return addressText; 
     } else { 
      return "No address found by the service: Note to the developers, If no address is found by google itself, there is nothing you can do about it."; 
     } 
    } else { 
     return "Location Not available"; 
    } 

} 



/** 
* get latitude 
* 
* @return latitude in double 
*/ 
public double getLatitude() { 
    if (mLocation != null) { 
     mLatitude = mLocation.getLatitude(); 
    } 
    return mLatitude; 
} 

/** 
* get longitude 
* 
* @return longitude in double 
*/ 
public double getLongitude() { 
    if (mLocation != null) { 
     mLongitude = mLocation.getLongitude(); 
    } 
    return mLongitude; 
} 

/** 
* close GPS to save battery 
*/ 
public void closeGPS() { 
    if (mLocationManager != null) { 
     try { 
      mLocationManager.removeUpdates(GPSService.this); 
     } catch (SecurityException e) { 
      Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED"); 
     } 
    } 
} 

/** 
* show settings to open GPS 
*/ 
public void askUserToOpenGPS() { 
    AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(mContext); 

    // Setting Dialog Title 
    mAlertDialog.setTitle("Location not available, Open GPS?") 
      .setMessage("Activate GPS to use use location services?") 
      .setPositiveButton("Open Settings", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        mContext.startActivity(intent); 
       } 
      }) 
      .setNegativeButton("Cancel",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }).show(); 
} 

/** 
* Updating the location when location changes 
*/ 
@Override 
public void onLocationChanged(Location location) { 
    mLatitude = location.getLatitude(); 
    mLongitude = location.getLongitude(); 
} 

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

} 

Я также добавил эти коды в Android Manifest:

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

ответ

0

Укажите следующие разрешения в вашем файле манифеста

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

и проверить разрешение на запуск для API 23 выше

if (Build.VERSION.SDK_INT >= 23 && 
     ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
     ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return null; 
    } 
+0

жаль ~ я добавил эти коды .. и до сих пор тот же – user6868737

+0

возвращение там есть красная линия: отсутствует возвращаемое значение – user6868737

+0

где вы используете этот код? –

0

Попытайтесь проверить разрешение на размещение и спросить об этом, используя этот код.

int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION); 
      int permissionCheck1 = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION); 
      if (permissionCheck == PackageManager.PERMISSION_GRANTED && permissionCheck1 == PackageManager.PERMISSION_GRANTED) { 
       startService(new Intent(MainActivity.this, TrackingService.class)); 
       locationRelatedServices(); 
      } else { 
       ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}, PermissionInts.LOCATION_CODE); 
      } 

После того, как пользователь дает разрешение, вы можете использовать этот код для запуска проекта:

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    switch (requestCode) { 
     case PermissionInts.LOCATION_CODE: 
        locationRelatedServices(); 
      } 
      break; 

} 
+0

спасибо за помощь !!! ^^ – user6868737

+0

вы можете установить этот ответ на полезный @ user6868737 –

0

Ваш метод возвращает объект Location, когда вы не могли бы принести место, то вам нужно возвратить null (когда разрешение не предоставлено).

Использование

return null;, а не return;

+0

спасибо за помощь !!! ^^ – user6868737

0

Используйте этот исходный код

// Включает разрешение местоположения для Зефир

public void getPermissionLocation(){ 
    // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid 
    // checking the build version since Context.checkSelfPermission(...) is only available 
    // in Marshmallow 
    // 2) Always check for permission (even if permission has already been granted) 
    // since the user can revoke permissions at any time through Settings 
    if (ContextCompat.checkSelfPermission(PrimecardSplashscreen.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(PrimecardSplashscreen.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     // The permission is NOT already granted. 
     // Check if the user has been asked about this permission already and denied 
     // it. If so, we want to give more explanation about why the permission is needed. 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       if (shouldShowRequestPermissionRationale(
         Manifest.permission.ACCESS_FINE_LOCATION)) { 
        // Show our own UI to explain to the user why we need to read the contacts 
        // before actually requesting the permission and showing the default UI 
       } 
      } 
     } 

     // Fire off an async request to actually get the permission 
     // This will show the standard permission request dialog UI 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        AppData.READ_LOCATION_PERMISSIONS_REQUEST); 
     } 
    } 

} 


@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 


    // Make sure it's our original ACCESS_FINE_LOCATION request 
    if (requestCode == AppData.READ_LOCATION_PERMISSIONS_REQUEST) { 
     if (grantResults.length == 1 && 
       grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      Toast.makeText(PrimecardSplashscreen.this, "Read Location permission granted", Toast.LENGTH_SHORT).show(); 
     } else { 
      Toast.makeText(PrimecardSplashscreen.this, "Read Location permission denied", Toast.LENGTH_SHORT).show(); 
     } 
    } else { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    } 
} 
+0

спасибо за помощь !!! ^^ – user6868737

 Смежные вопросы

  • Нет связанных вопросов^_^