我有一个Android应用程序,可将 GPS 位置发送到数据库。
我今天对其进行了测试,发现位置管理员需要获得调用requestLocationUpdates
的权限。
我想设置允许的权限,我不想要求设备或用户许可。
现在,代码需要添加一些代码来请求权限吗?
/**
* Enable location services
*/
public void connect() {
Log.d(TAG, ".connect() entered");
// Check if location provider is enabled
String locationProvider = LocationManager.NETWORK_PROVIDER;
if (!locationManager.isProviderEnabled(locationProvider)) {
Log.d(TAG, "Location provider not enabled.");
app.setCurrentLocation(null);
return;
}
// register for location updates, if location provider and permission are available
String bestProvider = locationManager.getBestProvider(criteria, false);
if (bestProvider != null) {
locationManager.requestLocationUpdates(bestProvider, Constants.LOCATION_MIN_TIME, Constants.LOCATION_MIN_DISTANCE, this);
app.setCurrentLocation(locationManager.getLastKnownLocation(locationProvider));
}
}
/**
* Disable location services
*/
public void disconnect() {
Log.d(TAG, ".disconnect() entered");
String locationProvider = LocationManager.NETWORK_PROVIDER;
if (locationManager.isProviderEnabled(locationProvider)) {
locationManager.removeUpdates(this);
}
}
我也加入了清单。 PS:这个权限是新要求的,因为我几天前在相同的设备(Android 4.4)上测试了应用程序并且运行良好。 自2017/08/21以来,Android上的某些默认权限是否有可能发生变化?如果是这样,我怎么能改变它?
答案 0 :(得分:1)
你必须在Marshmallow版本的android上请求运行时的许可。完成此link
从Android 6.0(API级别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;
}
答案 1 :(得分:0)
这是检查权限的最佳库 的 com.github.fccaikai:AndroidPermissionX:1.0.0 强>
示例代码:
private void openROMToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog1.Title = "Select A ROM File";
OpenFileDialog1.InitialDirectory = "C:/Users";
OpenFileDialog1.ShowDialog();
textBox1.Text = System.IO.Path.GetFileName(OpenFileDialog1.FileName);
textBox8.Text = "Calculating...";
{
backgroundWorker1.RunWorkerAsync(OpenFileDialog1.FileName);
}
string getExt = Path.GetExtension(OpenFileDialog1.FileName);
getExt = getExt.ToLower();
if (getExt == ".smd" || getExt == ".gen" || getExt == ".md")
{
// SEGA GENESIS CODE (READ ROM DATA GOES HERE)
}
// REPEAT "if" STATEMENT FOR NEXT FILE FORMAT
答案 2 :(得分:0)
您必须编写一种方法来检查用户是否提供了权限。如果没有,请询问用户是否允许。 Constants.LOCATION_PERMISSION_REQUEST只是在onRequestpermission结果中获得回调时匹配的请求代码
public static boolean checkLocationPermission(Activity activity){
if(ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION}, Constants.LOCATION_PERMISSION_REQUEST);
return false;
}
return true;
}
在用户允许的情况下,您将获得重写方法“onRequestPermissionsResult()”的回调