Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 添加Geofence会导致ApiException状态13_Android_Google Play Services_Android Pendingintent_Android Geofence - Fatal编程技术网

Android 添加Geofence会导致ApiException状态13

Android 添加Geofence会导致ApiException状态13,android,google-play-services,android-pendingintent,android-geofence,Android,Google Play Services,Android Pendingintent,Android Geofence,最近遇到了一个问题,添加GeoFence将随机失败,并出现一个ApiException,状态代码13,定义为“错误”,没有提供额外的详细信息 通常,错误代码是特定于地理围栏的,但这似乎是一个一般性的故障。有人知道GeofencingClient为什么会返回状态代码13吗?我找不到任何人有同样的问题 这似乎影响了应用程序的较旧版本,以前它在那里工作时没有问题 我已经仔细检查了纬度/经度是否为有效坐标,并且相同的数据有时可以正常工作。我尝试过用不同的API键初始化,以防地图/位置服务出现问题,但没有

最近遇到了一个问题,添加GeoFence将随机失败,并出现一个ApiException,状态代码13,定义为“错误”,没有提供额外的详细信息

通常,错误代码是特定于地理围栏的,但这似乎是一个一般性的故障。有人知道GeofencingClient为什么会返回状态代码13吗?我找不到任何人有同样的问题

这似乎影响了应用程序的较旧版本,以前它在那里工作时没有问题

我已经仔细检查了纬度/经度是否为有效坐标,并且相同的数据有时可以正常工作。我尝试过用不同的API键初始化,以防地图/位置服务出现问题,但没有任何区别

我也试着从
GeofencingClient(context)
LocationServices.getGeofencingClient(context)
没有更改

我尝试过升级库版本,没有区别

定义了播放服务版本和API密钥的典型清单设置

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version"/>

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/maps_api_key"/>

<service android:name=".GeofenceTransitionsIntentService"/>
直到最近,这才成为一个问题,现在几乎100%的时间都在发生

com.google.android.gms.common.api.ApiException: 13:


编辑:从PlayServices 17.7.85版(在我的设备上)开始,这个问题似乎在谷歌端得到了解决

我正在使用Android Q Beta进行一些测试,并注意到如果我的应用程序具有新的“仅允许在使用应用程序时”位置权限,我总是会遇到这个错误。即使在应用程序处于前台时,我也无法添加任何地理围栏,其位置权限设置为“仅在使用应用程序时允许”,但只要我将应用程序的位置权限设置为“始终允许”,我就可以添加地理围栏了

然而,我仍然在其他API版本上看到这个问题,所以不幸的是,它并没有完全解决这个问题。我有一台运行安卓8.0的设备和一台运行安卓9.0的设备,我仍然偶尔看到状态错误#13。我不知道该在这些设备上做什么,因为它们没有新的“仅允许在使用应用程序时使用”权限。它们都具有完全的位置访问权限

我之所以添加这个答案,是因为它可能会帮助一些运行Android Q测试版的用户,或者至少让他们了解问题的原因。我确实认为这与最新的Google Play服务版本有关,因为我认为我们都做得很好

编辑: 我已经在谷歌的问题追踪器上添加了一个问题,如果有人对以下内容感兴趣。。。

尽管没有对应用程序进行任何更改,但我和我的应用程序用户开始经常发生
addgeofenses
StatusException:13
错误。在运行Android 9的各种设备上观察到。用户大约在6月中旬开始报告该问题

在这里阅读了关于它的内容(感谢
@Ldawg
@stepheruda
)并在谷歌问题上添加了我的评论之后,我决定在添加地理围栏时对应用程序进行一些推测性的更改。做了一些测试,在问题不可复制后,发布了Beta版,然后生产版发布到Play store。新版本尚未发现该问题

我正在发布修改后的解决方案,以防它对任何人都有帮助,但是它在概念上与最初发布的带有错误的
Ldawg
类似。另外,我还没有能够对以前的版本进行回归测试,看看它是否仍然像以前一样出现。由于这是一个没有描述的不透明错误,因此gms中存在由不同状态引起的多个触发器并非不可想象。除非谷歌自己证实这一点,否则修复方案是推测性的,这个问题有些难以捉摸

我的解决方案的基本变化是,
rxLocationProvider.addgeofines
rxLocationProvider.removeGeofences
在循环中为每个位置多次调用,并观察到
状态13
错误

更改后,
rxLocationProvider.addgeofenses
仅被调用一次,其中包含
geofense
对象列表
rxLocationProvider.removeGeofences也会获取位置列表,而不是单个位置。该应用程序使用mcharmas android反应式位置库
https://github.com/mcharmas/Android-ReactiveLocation

使用StatusException:13之前的代码

private GeofencingRequest createGeofencingRequest(String requestId) {
        Geofence geofence = new Geofence.Builder()
                .setRequestId(requestId)
                .setCircularRegion(stationInfo.getLat(), stationInfo.getLon(), GEOFENCE_RADIUS_METRES)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build();
        return new GeofencingRequest.Builder().addGeofence(geofence).build();
    }

public void addGeofence(String locationCrs) {
        final GeofencingRequest geofencingRequest = createGeofencingRequest(locationCrs);
        final PendingIntent pendingIntent = createNotificationBroadcastPendingIntent();

        ArrayList<String> lists = new ArrayList<>();
        lists.add(locationCrs);
        rxLocationProvider
                .removeGeofences(lists)
                .flatMap((Function<Status, Observable<Status>>)
                        status -> rxLocationProvider.addGeofences(pendingIntent, geofencingRequest))
                .subscribe(addGeofenceResult -> {

                }, throwable -> {
                    // **Status 13 here**
                }));
    }
私有GeofencingRequest createGeofencingRequest(字符串请求ID){
Geofence Geofence=新建Geofence.Builder()
.setRequestId(requestId)
.setCircularRegion(stationInfo.getLat()、stationInfo.getLon()、GEOFENCE_半径_米)
.setExpirationDuration(地理围栏永不过期)
.setTransitionTypes(geofines.geofines_TRANSITION_ENTER | geofines.geofines_TRANSITION_EXIT)
.build();
返回新的GeofencingRequest.Builder().addGeofence(geofence.build();
}
公共空间添加地理围栏(字符串位置CRS){
最终GeofencingRequest GeofencingRequest=创建GeofencingRequest(位置CRS);
final PendingEvent PendingEvent=CreateNotificationBroadcasting PendingEvent();
ArrayList list=新的ArrayList();
列表。添加(位置CRS);
rxLocationProvider
.removeGeofences(列表)
.flatMap((函数)
状态->rxLocationProvider.addGeofences(PendingEvent、geofencingRequest))
.订阅(addGeofenceResult->{
},可丢弃->{
//**此处为状态13**
}));
}
代码后-未观察到错误

private GeofencingRequest getGeofencingRequest() {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofences(geofenceList);
        return builder.build();
    }

private Geofence createGeofence(String requestId) {
        return new Geofence.Builder()
                .setRequestId(requestId)
                .setCircularRegion(stationInfo.getLat(), stationInfo.getLon(), GEOFENCE_RADIUS_METRES)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build();
    }

public void addGeofences(List<String> locs) {
        for (String loc : locs) {
            Geofence request = createGeofence(loc);
            geofenceList.add(request);
        }

        final PendingIntent pendingIntent = createNotificationBroadcastPendingIntent();

        disposable.add(rxLocationProvider
                .removeGeofences(locs)
                .flatMap((Function<Status, Observable<Status>>)
                        status -> rxLocationProvider.addGeofences(pendingIntent, getGeofencingRequest()))
                .subscribe(addGeofenceResult -> {

                }, throwable -> {
                    // **No Errors observed**
                }));
    }
private GeofencingRequest getGeofencingRequest(){
GeofencingRequest.Builder=新的GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL\u TRIGGER\u ENTER);
建造商。添加地理围栏(地理围栏列表);
返回builder.build();
}
私人土工围栏createGeofence(圣
private GeofencingRequest getGeofencingRequest() {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofences(geofenceList);
        return builder.build();
    }

private Geofence createGeofence(String requestId) {
        return new Geofence.Builder()
                .setRequestId(requestId)
                .setCircularRegion(stationInfo.getLat(), stationInfo.getLon(), GEOFENCE_RADIUS_METRES)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build();
    }

public void addGeofences(List<String> locs) {
        for (String loc : locs) {
            Geofence request = createGeofence(loc);
            geofenceList.add(request);
        }

        final PendingIntent pendingIntent = createNotificationBroadcastPendingIntent();

        disposable.add(rxLocationProvider
                .removeGeofences(locs)
                .flatMap((Function<Status, Observable<Status>>)
                        status -> rxLocationProvider.addGeofences(pendingIntent, getGeofencingRequest()))
                .subscribe(addGeofenceResult -> {

                }, throwable -> {
                    // **No Errors observed**
                }));
    }
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
Dexter.withActivity(this@Activity_Map)
        .withPermissions(
                Manifest.permission.ACCESS_COARSE_LOCATION
                ,Manifest.permission.ACCESS_FINE_LOCATION
                ,Manifest.permission.ACCESS_BACKGROUND_LOCATION
        )
        .withListener(object: MultiplePermissionsListener {
            override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
                report?.let {
                    if(report.areAllPermissionsGranted()){
                        aktifkangps()
                        ambilshareddprefnip()
                        geofencingClient = LocationServices.getGeofencingClient(this@Activity_Map)      //drawgeo()
                        cekInternet()
                        with(sharedPreferences) {
                            status = getString(loginmasuk.SP_NIP   , "0").toString()
                        }
                        if (status == "failed"){
                            val imap = Intent(this@Activity_Map, Activity_ambil_lokasi::class.java)
                            startActivity(imap)
                            finish()
                        }
            }