2016-05-27 3 views
0
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; 
} 

エラーが発生しました。私はエラーが発生しました。これはユーザーによって拒否される必要があります。解決できますか?Android逆gecode

マニフェストで

私は、許可のこれらのセットを提供してきた

+0

エラーが発生したときにスタックトレースを投稿してくださいことはできますか?ありがとうございました。 –

+0

@Rajesh vあなたのtargetSdkは何ですか? – Nisarg

+0

マニフェストファイルでaccess_fine_locationとAccess_coarse_locationの権限を追加します。テストにmarshmallow(または> SDK 21)を使用している場合は、実行時に動的に権限を追加する必要があります。https://developer.android.com/training/permissions/requesting.html –

答えて

0
private void checkPermission() { 
    if (ContextCompat.checkSelfPermission(this, 
     Manifest.permission.ACCESS_FINE_LOCATION) 
     != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, 
     Manifest.permission.ACCESS_COARSE_LOCATION) 
     != PackageManager.PERMISSION_GRANTED) { 

    ActivityCompat.requestPermissions(this, 
      new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 
      MY_PERMISSIONS_REQUEST_FINE_LOCATION); 

} else { 
    getLocation(); 
} 

}

@Override 
    public void onRequestPermissionsResult(int requestCode, 
            String permissions[], int[] grantResults)  { 
switch (requestCode) { 
    case MY_PERMISSIONS_REQUEST_FINE_LOCATION: { 
     // If request is cancelled, the result arrays are empty. 
     if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      // permission was granted, yay! Do the 
      // contacts-related task you need to do. 
     getLocation(); 
     } else { 
      // permission denied, boo! Disable the 
     } 
     return; 
    } 
} 
    } 
関連する問題