Android FusedLocationApi位置服务使用运行时权限API 23无法正常工作
有人能帮我解决这个问题吗Android FusedLocationApi位置服务使用运行时权限API 23无法正常工作,android,android-service,fusedlocationproviderapi,location-services,google-location-services,Android,Android Service,Fusedlocationproviderapi,Location Services,Google Location Services,有人能帮我解决这个问题吗 onConnected() LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this); 代码不执行;并从checkSelfPermission()重新运行 如何才能在此代码中正常工作FusedLocationApi 我的代码在下面 public class JMSCLocationService
onConnected()
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
代码不执行;并从checkSelfPermission()重新运行
如何才能在此代码中正常工作FusedLocationApi
我的代码在下面
public class JMSCLocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private final String TAG = "MyAwesomeApp";
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
// Check Permissions Now
private static final int REQUEST_LOCATION = 2;
//private final double radiusInMeters = 1609.34;
private final double radiusInMeters = 16.34;
LatLng firstLatLong;
Location fLoc;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "GoogleApiClient connection has STARTED");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
firstLatLong = new LatLng(0, 0);
Log.i(TAG, "GoogleApiClient connection has STARTED");
}
@Override
public void onDestroy() {
// disconnect the client.
if (mGoogleApiClient != null && mGoogleApiClient.isConnected())
mGoogleApiClient.disconnect();
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Connect the client.
if (mGoogleApiClient != null && !mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
Log.i(TAG, "GoogleApiClient mGoogleApiClient.connect();");
}
if (mGoogleApiClient.isConnected()) {
Log.i(TAG, "GoogleApiClient mGoogleApiClient.isConnected();");
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000); // Update location every second
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "GoogleApiClient connection has been suspend");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.i(TAG, "GoogleApiClient connection has failed");
}
@Override
public void onLocationChanged(Location mCurrentLocation) {
Log.i(TAG, "GoogleApiClient Location received: " + mCurrentLocation.toString());
//test outside
double mLatitude = mCurrentLocation.getLatitude();
double mLongitude = mCurrentLocation.getLongitude();
if(firstLatLong.latitude == 0.0 && firstLatLong.longitude == 0.0){
firstLatLong = new LatLng(mLatitude,mLongitude);
fLoc=new Location(mCurrentLocation);
}
float[] results = new float[1];
Location.distanceBetween(firstLatLong.latitude, firstLatLong.longitude,
mLatitude, mLongitude, results);
float distanceInMeters = fLoc.distanceTo(mCurrentLocation);
try{
if( distanceInMeters > radiusInMeters ){
Toast.makeText(getBaseContext(), "Outside, distance from center", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Inside, distance from center", Toast.LENGTH_LONG).show();
}
}catch (Exception ex){
ex.printStackTrace();
}
}
}
您必须请求权限。您只是在检查您的应用程序是否具有该权限
您正在忽略代码中的checkSelfPermission()
的结果
阅读评论,它们是不言自明的
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// 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;
} else {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
阅读有关从以下位置请求运行时权限的信息:
编辑1
我认为,…但在服务的编译时,它给出了必须使用自检权限的错误您指的是Lint警告
您可以通过在调用方法的顶部添加@SuppressWarnings(“MissingPermission”)
来抑制它们
但是,如果您有权限,请确保您正在调用requestLocationUpdates()
(注意if..else
块)。使用:
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_ID);
}else{
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
以及:
@覆盖
public void onRequestPermissionsResult(int-requestCode,字符串权限[],int[]grantResults){
开关(请求代码){
个案申请编号:{
for(int i=0;i
我使用了扩展服务,这是不允许我@Override此方法的,然后当您启动服务时调用此方法。如果您有权限,则启动服务我已获得活动的权限,但当我运行代码方法时,如果我删除selfcheck()则返回自selfcheck()yse从服务获取权限,仅从活动获取权限它的give error to must use self check或secutiryexception the it give SecurtyException in printstackstracei已使用服务而非活动获取位置我如何在服务中执行此操作您必须在启动服务
。只有在用户授予权限时才启动服务。是的,但我已从ACTIVITY获得权限,但在服务的编译时,它为必须使用自检权限提供了错误您是否已在块中添加LocationServices.FusedLocationApi.RequestLocationUpdate(…)
?
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_ID: {
for( int i = 0; i < permissions.length; i++ ) {
if( grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Log.d("Permissions", "Permission Granted: " + permissions[i]);
} else if( grantResults[i] == PackageManager.PERMISSION_DENIED ) {
Log.d( "Permissions", "Permission Denied: " + permissions[i] );
}
}
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}