在Android应用程序中获取用户的位置权限

时间:2017-06-05 13:31:32

标签: java android permissions location

我正在尝试使用typicaly权限框获取用户的实际位置: 像这样: enter image description here

如果有人知道怎么做,请求答案。

3 个答案:

答案 0 :(得分:8)

请求运行时使用设备当前位置的权限,如下所示:

if (ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(YourActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
      return;
}else{
  // Write you code here if permission already given.
} 

答案 1 :(得分:3)

你可以这样做

在清单中:

<uses-feature android:name="android.hardware.location.gps" />

在您需要此权限请求的活动中:

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

然后使用此功能让用户回答:

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
    case 1: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        } else {
            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
    return;
    }
        // other 'case' lines to check for other
        // permissions this app might request
}
}

如果用户接受一次,那么您的应用程序将记住它,您将不再需要发送此DialogBox。请注意,如果他决定,用户可以稍后禁用它。然后在请求位置之前,您必须测试是否仍然授予了权限:

 public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}

答案 2 :(得分:1)

首先,您需要编写一个类,您将在其中定义用于授予和检查用户位置权限的方法。

public class CheckPermission {

    //  CHECK FOR LOCATION PERMISSION
    public static boolean checkPermission(Activity activity){
        int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION);
        if (result == PackageManager.PERMISSION_GRANTED){

            return true;

        } else {

            return false;

        }
    }

    //REQUEST FOR PERMISSSION
    public static void requestPermission(Activity activity, final int code){

        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.ACCESS_FINE_LOCATION)){

            Toast.makeText(activity,"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},code);
        }
    }

}

现在,您可以调用以上两种静态方法来检查或授予您的活动权限。

public class LocationActivity extends AppCompatActivity {


    private GPSTracker mytracker;
    private Location myLocation;
    private double latitude=0.0d,longitude=0.0d;
    public final static int TAG_PERMISSION_CODE=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        if(!CheckPermission.checkPermission(LocationActivity.this)) {
          CheckPermission.requestPermission(SplashScreenActivity.this,TAG_PERMISSION_CODE);

        } 
    }
}