尝试设置地理围栏时 GEOFENCE_NOT_AVAIBLE(代码 1000)

Dom*_*min 4 java android geolocation android-geofence

该问题发生在比 Oreo 更早的 Android 以及 Oreo 和更新版本上。

即使完成了以下步骤,我也无法让地理围栏工作:

  • 定位服务设置为高精度
  • 已启用 Wi-Fi 和移动数据
  • 应用程序被授予位置权限
  • Google 服务已添加到项目中
  • Google 服务和 Play 商店是最新的并已安装在设备上
  • 禁用电池优化(测试目的)

我用下面的代码,如果检查GPS_PROVIDERNETWORK_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");
    }

}
Run Code Online (Sandbox Code Playgroud)

以上两个都给了我积极的结果,所以问题不可能在这里。

确切的错误:

电子/地理围栏: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());
Run Code Online (Sandbox Code Playgroud)

我使用以下代码设置地理围栏:

            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");
            }

        }
    });
}
Run Code Online (Sandbox Code Playgroud)

此代码与 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"/>
Run Code Online (Sandbox Code Playgroud)

摇篮:

implementation 'com.google.android.gms:play-services-maps:16.0.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
Run Code Online (Sandbox Code Playgroud)

任何人都可以看到我本可以做的事情吗?提前致谢!

And*_*ndy 6

好的,这是基于您的 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();

    }

}
Run Code Online (Sandbox Code Playgroud)

经过一番调查,我发现我可以使用此代码示例重新创建 1000 错误代码。它基于此论坛帖子:https : //androidforums.com/threads/error-adding-geofence-on-android-8.1289302/

所以按照这些指示(修复 - 但我翻转它们重新创建然后修复):

使用手机“设置 | 安全和位置 | 位置 | 模式” - 在“高精度、省电或仅设备”之间切换,直到收到此提示(设置路径将因 android 版本而异):

在此处输入图片说明

在此示例代码中 - 如果您以“DISAGREE”响应,示例代码将生成 1000 错误代码;如果您重复并回复“同意”,将成功添加地理围栏。