【发布时间】:2019-05-28 12:01:05
【问题描述】:
该问题出现在比 Oreo 更早的 Android 以及 Oreo 和更新版本的 Android 上。
即使完成了以下步骤,我也无法让地理围栏工作:
- 定位服务设置为高精度
- Wi-Fi 和移动数据已启用
- 应用程序被授予位置权限
- Google 服务已添加到项目中
- Google 服务和 Play 商店是最新的并已安装在设备上
- 禁用电池优化(测试目的)
如果启用了GPS_PROVIDER 和NETWORK_PROVIDER,我已经检查了以下代码:
@Override
protected void onResume() {
super.onResume();
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.e("Provider", "Provider is not avaible");
} else if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.v("Provider", "GPS Provider is avaible");
}
if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
Log.e("Network Provider", "Provider is not avaible");
} else if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
Log.v("Network Provider", "provider is avaible");
}
}
以上都给了我积极的结果,所以问题不可能在这里。
确切的错误:
电子/地理围栏:com.google.android.gms.common.api.ApiException:1000:
我在onCreate的开头设置了mGeofencingClient:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGeofencingClient = LocationServices.getGeofencingClient(getApplicationContext());
我使用以下代码设置地理围栏:
mGeofenceList.add(
new Geofence.Builder()
.setRequestId("blablabla")
.setCircularRegion(50.32, 43.23, 232)
.setExpirationDuration(-1L)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
// }
PermissionCheck mPermissionCheck = new PermissionCheck();
if (!mPermissionCheck.isPermissionGranted(getApplicationContext())){
mPermissionCheck.askForPermission(MainActivity.this);
return;
}
setGeofences();
}
private GeofencingRequest getGeofencingRequest(){
if (mGeofenceList.isEmpty()){
return null;}
Log.v("mGeofenceList", mGeofenceList.toString());
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER |
GeofencingRequest.INITIAL_TRIGGER_EXIT);
builder.addGeofences(mGeofenceList);
return builder.build();
}
private PendingIntent getGeofencePendingIntent(){
if (mGeofencePendingIntent != null){
return mGeofencePendingIntent;
}
Intent intent = new Intent(getApplicationContext(), Geofencing.class);
mGeofencePendingIntent = PendingIntent.getService(getApplication(),
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
@SuppressLint("MissingPermission")
private void setGeofences(){
GeofencingRequest geofencingRequest = getGeofencingRequest();
PendingIntent pi = getGeofencePendingIntent();
mGeofencingClient.addGeofences(geofencingRequest, pi)
.addOnSuccessListener(MainActivity.this, new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("Geofences", "geofencing set up succesfully");
Toast.makeText(MainActivity.this, "Geofences set up", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(MainActivity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("Geofence", e.toString());
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.e("Provider", "Provider is not avaible");
}
if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
Log.e("Network Provider", "Provider is not avaible");
}
}
});
}
此代码与 Google 文档中的代码几乎相同。 清单权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.location.network"/>
<uses-feature android:name="android.hardware.location.gps"/>
分级:
implementation 'com.google.android.gms:play-services-maps:16.0.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
谁能看到我本可以做的事情? 提前致谢!
【问题讨论】:
-
您在什么时候设置
mGeofencingClient以及您提供哪个上下文? -
我已经更新了代码。我在
onCreate的开头设置mGeofencingClient并提供getApplicationContext。 -
您是否已将 LocationServices 添加到 GoogleApiClient -(我只是在检查我的应用程序所做的事情的清单 - 无需发布代码)(1000 是 API 错误代码)?
-
您的意思是 Gradle 实现还是我应该以编程方式将 LocationServices 添加到 GoogleApiClient?如果以编程方式(在代码中)我没有,因为我使用了 LocationManager
-
好吧,我把这里弄得有点乱。在我的代码中,我有 LocationManager,它从
GPS_PROVIDER和NETWORK_PROVIDER请求 LocationUpdates。我没有GoogleApiClient的实例。 1000 显示为 API 错误代码
标签: java android geolocation android-geofence