该问题发生在比Oreo以及旧版Oreo和新版Android上。
即使完成以下步骤,我也无法运行地理围栏:
我已使用以下代码检查是否启用了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");
}
}
以上两者都给了我积极的结果,所以问题不可能在这里。
确切错误:
E / Geofence:com.google.android.gms.common.api.ApiException:1000:
我在mGeofencingClient
的开头设置了onCreate
:
@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'
谁能看到我本可以做的错误? 预先感谢!
答案 0 :(得分:1)
好的,这是一个基于您的OP的地理围栏最小工作程序-只是为了排除代码的实现-还为其他测试实现了两个其他接口,因此请忽略。
“正在工作”表示已成功添加地理围栏。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {
private List<Geofence> mGeofenceList = new ArrayList<>();
private GeofencingClient gfc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
gfc = LocationServices.getGeofencingClient(getApplicationContext());
mGeofenceList.add(new Geofence.Builder().setRequestId("aa").setCircularRegion(50.32, 43.23, 232).setExpirationDuration(-1L).setTransitionTypes(
Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT).build());
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Check Permissions Now
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
1);
}
else {
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 mGeofencePendingIntent;
private PendingIntent getGeofencePendingIntent(){
if (mGeofencePendingIntent != null){
return mGeofencePendingIntent;
}
Intent intent = new Intent(getApplicationContext(), Object.class);
mGeofencePendingIntent = PendingIntent.getService(getApplication(),
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
@SuppressLint("MissingPermission")
private void setGeofences(){
GeofencingRequest geofencingRequest = getGeofencingRequest();
PendingIntent pi = getGeofencePendingIntent();
gfc.addGeofences(geofencingRequest, pi)
.addOnSuccessListener(this, new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("Geofences", "geofencing set up succesfully");
Toast.makeText(MapsActivity.this, "Geofences set up", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(MapsActivity.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");
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
setGeofences();
}
}
经过一番调查,我发现可以使用此代码示例重新创建1000个错误代码。它基于以下论坛帖子:https://androidforums.com/threads/error-adding-geofence-on-android-8.1289302/
因此,请遵循这些指示(进行修复-但我将其翻转以重新创建然后修复):
使用手机“设置|安全和位置|位置|模式”-在“高精度,省电或仅设备”之间切换,直到出现此提示(设置路径会因Android版本而异):
在此示例代码中-如果您回答“ DISAGREE”,则示例代码将生成1000错误代码;如果重复并回答“ AGREE”,则将成功添加地理围栏。
答案 1 :(得分:0)
对于 android oreo 到 android S 确保访问设置高优先级,因为地理围栏需要它或它成为错误 1000
fun ceksetting(){
val builder = LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest)
val locationRequest = LocationRequest()
locationRequest!!.interval = 50000
locationRequest!!.fastestInterval = 50000
locationRequest!!.smallestDisplacement = 170f // 170 m = 0.1 mile
locationRequest!!.priority = LocationRequest.PRIORITY_HIGH_ACCURACY //set according to your app function
val client: SettingsClient = LocationServices.getSettingsClient(requireActivity())
val task: Task<LocationSettingsResponse> = client.checkLocationSettings(builder.build())
task.addOnSuccessListener { locationSettingsResponse ->
//here call your geofence
}
task.addOnFailureListener { exception ->
if (exception is ResolvableApiException){
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
exception.startResolutionForResult(requireActivity(),
REQUEST_CHECK_SETTINGS)
} catch (sendEx: IntentSender.SendIntentException) {
// Ignore the error.
}
}
}
}