- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有很多类似的问题被问到,但似乎没有一个能解决我的问题。
我已经应用了文档中提到的运行时权限,但仍然收到运行时错误
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
这是我的完整代码。
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,LocationListener {
private static final String TAG ="hello" ;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public Location mLastLocation=null;
public final int MY_PERMISSIONS_REQUEST_COARSE_LOCATION=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (mGoogleApiClient==null){
mGoogleApiClient= new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 100) // 10 seconds, in milliseconds
.setFastestInterval( 1* 100); // 1 second, in milliseconds
}
@Override
public void onConnected(@Nullable Bundle bundle) {
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
int permissionCheck1 = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION);
if (permissionCheck != PackageManager
.PERMISSION_GRANTED && permissionCheck1 != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_COARSE_LOCATION);
}
}
mLastLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
TextView mLatitudeText =(TextView)findViewById(R.id.textView);
TextView mLongitudeText =(TextView)findViewById(R.id.textView2);
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
Log.i("Location Info", "Location achieved!");
} else {
Log.i("Location Info", "No location :(");
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);//error here!!
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
protected void onResume() {
mGoogleApiClient.connect();
super.onResume();
}
@Override
protected void onPause() {
mGoogleApiClient.disconnect();
super.onPause();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_COARSE_LOCATION :{
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("Message","Got full permission");
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
@Override
public void onLocationChanged(Location location)
{
Log.i("Location Info", "Location is changed Bitchh :(");
handleNewLocation(location);
}
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
}
}
此外,我在 list 文件中已授予权限。
所以,这是运行时的错误堆栈跟踪
10-24 17:13:02.857 2224-2379/com.example.ashutosh.location E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare() 0-24 17:13:03.184 2224-2224/com.example.ashutosh.location E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ashutosh.location, PID: 2224 java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations. at android.os.Parcel.readException(Parcel.java:1620) at android.os.Parcel.readException(Parcel.java:1573) at com.google.android.gms.internal.zzed.zzb(Unknown Source) at com.google.android.gms.internal.zzcda.zza(Unknown Source) at com.google.android.gms.internal.zzcdd.zza(Unknown Source) at com.google.android.gms.internal.zzcdj.zza(Unknown Source) at com.google.android.gms.internal.zzccc.zza(Unknown Source) at com.google.android.gms.internal.zzbay.zzb(Unknown Source) at com.google.android.gms.internal.zzbca.zze(Unknown Source) at com.google.android.gms.internal.zzbcx.zze(Unknown Source) at com.google.android.gms.internal.zzbcp.zze(Unknown Source) at com.google.android.gms.internal.zzccb.requestLocationUpdates(Unknown Source) at com.example.ashutosh.location.MainActivity.onConnected(MainActivity.java:122) at com.google.android.gms.common.internal.zzac.zzn(Unknown Source) at com.google.android.gms.internal.zzbcp.zzm(Unknown Source) at com.google.android.gms.internal.zzbcd.zzpY(Unknown Source) at com.google.android.gms.internal.zzbcd.onConnected(Unknown Source) at com.google.android.gms.internal.zzbcx.onConnected(Unknown Source) at com.google.android.gms.internal.zzbbi.onConnected(Unknown Source) at com.google.android.gms.common.internal.zzaa.onConnected(Unknown Source) at com.google.android.gms.common.internal.zzn.zzrj(Unknown Source) at com.google.android.gms.common.internal.zze.zzs(Unknown Source) at com.google.android.gms.common.internal.zzi.zzrk(Unknown Source) at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:152) at android.app.ActivityThread.main(ActivityThread.java:5497) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
最佳答案
requestPermissions()
是异步。当它返回时,您还没有获得许可。用户甚至没有被询问过权限。
您需要在两个地方执行位置逻辑:
提前,如果您已获得许可
在 onRequestPermissionResult()
中,如果您请求权限且用户授予了权限
This sample app和 this sample app说明如何请求运行时权限并将其与 LocationClient
一起使用。
关于java - 客户端必须具有 ACCESS_FINE_LOCATION 权限才能请求 PRIORITY_HIGH_ACCURACY 位置运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46909987/
我的 Android 应用需要高精度位置跟踪。在应用启动时,如果未选择高精度,它会以编程方式读取位置设置并显示一个屏幕。 我将 Google 的官方示例 ( https://developers.go
我正在使用 FusedLocationAPI 获取高精度位置更新(更新间隔为 2 秒,最快间隔为 5 秒)。大多数时候它工作正常。但是,有时它会给出 1200 米这样的精度。 我知道一开始它可能会发生
有很多类似的问题被问到,但似乎没有一个能解决我的问题。 我已经应用了文档中提到的运行时权限,但仍然收到运行时错误 LocationServices.FusedLocationApi.requestLo
我们对同一情况下仅连接到 WiFi 的设备每 10 分钟请求位置所消耗的电池电量进行了基准测试 恢复出厂设置 相同的起始电池电量 没有安装额外的应用 首先将优先级设置为 PRIORITY_BALANC
Android Fused Location API - 如何在一个应用程序中使用 PRIORITY_BALANCED_POWER_ACCURACY 和 PRIORITY_HIGH_ACCURACY?
我最近在 android 版本 6.0 的设备上遇到了一个问题 我遇到以下崩溃 java.lang.SecurityException: Client must have ACCESS_FINE_LO
我是一名优秀的程序员,十分优秀!