android - 在运行时获取权限 (ACCESS_FINE_LOCATION) 的问题
问题描述
我编写了以下代码来获取用户位置,但应用程序不断崩溃。这可能与我使用的 Android Studio 版本有关。
package com.example.userlocationmap;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length >= 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);//get the location locater service running
locationListener = new LocationListener() {//listening in on your current location
@Override
public void onLocationChanged(Location location) {
//when location is changed
Log.i("location", location.toString());//printing location to logs
Toast.makeText(MainActivity.this, location.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
//when location is enabled on mobile
}
@Override
public void onProviderDisabled(String provider) {
//when location is disabled on mobile
}
};
//checking if the version of android is before marshmellow (SDK version 23)
if (Build.VERSION.SDK_INT < 23) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
//checking whether we have permission to access the user location
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_DENIED) {
//checking if permission is not granted. if not granted then ask for it.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
//this asks for permission which is stored in an array String
} else {
//if we already have permission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
}
请帮我解决这个问题。请告诉我需要进行哪些更改才能使代码可以在模拟器上运行。
解决方案
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length >= 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);//after getting permission get locationManager
if(locationManager != null)// prevent NullPointerException
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
推荐阅读
- python - 正则表达式用前一天替换一天
- c# - 如何解析 XMLDocument InnerText/InnerXML 字符串
- python - Pyspark 雪花连接器错误与 Df.Show
- python - 数据框通过搜索子字符串来切片列内容
- c++ - 罗技 G Hub - g502 上的程序 LED?
- java - 从json反序列化数组数组
- c# - .net core 不能在 api 中接受多个 IFormFile
- reactjs - 如何将 RichUtils 与 React Hooks 一起使用
- sql - Hive 的 SQL 查询失败
- in-app-purchase - 关于取消自动续订订阅的问题