位置Listerner - 呼叫需要得到用户可能拒绝的许可

时间:2017-04-23 15:39:26

标签: android locationmanager locationlistener

在以前的应用程序中使用此代码,一切似乎都好 - 但今天我得到了

通话需要得到用户可能拒绝的许可

我添加了

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

要显示并阅读有关该主题的几个主题,但仍然存在问题。你们能帮忙吗?

public class MainActivity extends AppCompatActivity implements LocationListener
{
    private LocationManager locationManager;
    public static String locationlong;
    public static String locationlati;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = (TextView)findViewById(R.id.pig);
        TextView cow = (TextView)findViewById(R.id.cow);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1, this);
    }
}

2 个答案:

答案 0 :(得分:0)

基本上因为android 6你需要在运行时询问某些权限,所以用户可以拒绝它们。

使用checkSelfPermission(..)来请求它并覆盖onRequestPermissionsResult以查看答案。

以下是完整示例的链接:

https://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:0)

在您的活动中添加权限检查:

你的代码中的

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tv = (TextView) findViewById(R.id.pig);
    TextView cow = (TextView) findViewById(R.id.cow);
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 0);
    }
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (checkLocationPermission()) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1, this);
    }


}

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

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

        case 0: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

@Override
public void onLocationChanged(Location location) {

}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}