我正在尝试使用设备中的弹出窗口激活设备上的位置,而无需进入设置页面;我在堆栈溢出上尝试了多种解决方案,但似乎没有一种解决方案可以打开我的模拟器或设备位置 我的位置代码如下:
OnRequest:
@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_GRANTED){
permission. its an array becayse it can be multiple permissions
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, locationListener);
}
}
}
主要:
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i("location",""+location.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION} ,1);
}else{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, locationListener);
}
清单:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
构建Gradle:
compile 'com.android.support:support-v4:25.0.0'
compile 'com.google.android.gms:play-services-maps:10.2.4'
compile 'com.google.android.gms:play-services-location:10.2.4'
答案 0 :(得分:0)
在您的主要课程中修改:
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION} ,1);
return;
}else{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, locationListener);
}
答案 1 :(得分:0)
1)删除与运行时权限相关的代码,并尝试在Marshmallow之前发布任何Android版本的设备上运行应用程序。通过这种方式,您将了解问题是否与运行时权限相关
2)您可以尝试使用此库来实现运行时权限。它非常简单易用。 https://github.com/Karumi/Dexter