这行代码要求我添加权限
dateselector
我该怎么做?
答案 0 :(得分:1)
试试这个,
private static final int REQUEST = 112;
if (Build.VERSION.SDK_INT >= 23)
{
String[] PERMISSIONS = {android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION};
if (!hasPermissions(mContext, PERMISSIONS))
{
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
} else {
getLocation();//call function
}
} else
{
getLocation();//call function
}
获取权限结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();//call function
} else {
Toast.makeText(mContext, "The app was not allowed to send sms.", Toast.LENGTH_LONG).show();
}
}
}
}
检查marshmallow的权限
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
清单
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
的getLocation
private void getLocation()
{
.
.
.
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
.
.
.
}