Android Wear:地理围栏 - ApiException:1000

13

我正在为Android Wear构建一个Android应用程序。 为了节省电量,我尝试使用地理围栏来跟踪您是否进入或退出某个位置。但是我无法使其工作。

首先,我不确定Android Wear是否支持Geofences?(独立运行?)我有一块包含GPS天线的华为手表2 LTE,并且我已经让FusedLocationClient工作了,所以我知道GPS可以使用。我还在手机上成功运行了我的地理围栏代码,没有任何问题。

运行代码时,我得到以下异常:

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

我在Google API文档中发现,这表示: GEOFENCE_NOT_AVAILABLE ,这并没有给我任何额外的信息。

这是我编写的启动和创建地理围栏的服务:

public class GeofencingService extends Service implements OnSuccessListener, OnFailureListener {

    private static final String TAG = "GeofencingService";
    private static final int NOTIFICATION_ID = 1;

    private GeofencingClient mGeofencingClient;
    private List<Geofence> mGeofenceList;
    private NotificationManager mNotificationManager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        mGeofencingClient = LocationServices.getGeofencingClient(this);
        mGeofenceList = new ArrayList<>();
        createGeofences();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showNofification();
        setupGeofence();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mNotificationManager.cancel(NOTIFICATION_ID);
    }

    private PendingIntent getGeofencePendingIntent()
    {
        Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
        return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    private GeofencingRequest getGeofencingRequest() {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();

        // The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
        // GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
        // is already inside that geofence.
        builder.setInitialTrigger(INITIAL_TRIGGER_ENTER | INITIAL_TRIGGER_EXIT);

        // Add the geofences to be monitored by geofencing service.
        builder.addGeofences(mGeofenceList);

        // Return a GeofencingRequest.
        return builder.build();
    }

    private void setupGeofence()
    {
        try{
            Log.i(TAG, "Setting up geofences...");
            mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent()).addOnSuccessListener(this).addOnFailureListener(this);
        } catch (SecurityException ex)
        {
            Log.d(TAG, "Exception: " + ex.getMessage());
        }
    }

    private void createGeofences()
    {
        Log.i(TAG, "Creating geofence...");
        mGeofenceList.add(new Geofence.Builder()
                // Set the request ID of the geofence. This is a string to identify this
                // geofence.
                .setRequestId("Test1")
                // Set the circular region of this geofence.
                .setCircularRegion(
                        50.03535,
                        4.33139,
                        100
                )
                .setExpirationDuration(NEVER_EXPIRE)
                // Set the transition types of interest. Alerts are only generated for these
                // transition. We track entry and exit transitions in this sample.
                .setTransitionTypes(GEOFENCE_TRANSITION_ENTER | GEOFENCE_TRANSITION_EXIT)
                // Create the geofence.
                .build());
    }

    private void showNofification()
    {
        Intent appIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, appIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, NotificationCompat.CATEGORY_SERVICE)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(getText(R.string.location_service_title))
                .setContentText(getText(R.string.location_service_text))
                .setLocalOnly(true)
                .setOngoing(true)
                .setContentIntent(contentIntent)
                .build();

        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }

    @Override
    public void onFailure(@NonNull Exception e) {
        Log.d(TAG, "Exception: " + e.getMessage());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setupGeofence();
            }
        }, 2000);
    }

    @Override
    public void onSuccess(Object o) {
        Log.d(TAG, "Success!");
    }
}

这里也有我写的示例的Github链接。

希望有人能帮我解决这个问题,因为我已经尝试过我所知道的一切了 :)


1000代表什么? - greenapps
1
@ greenapps,我在这个网站上找到了:https://developers.google.com/android/reference/com/google/android/gms/location/GeofenceStatusCodes 它的意思是:GEOFENCE_NOT_AVAILABLE。 - kevingoos
好的。然后请评论。 - greenapps
@ greenapps 那只是异常情况。没有解决方案吗? - kevingoos
那不是一条注释。你应该对“地理围栏不可用”的消息进行注释。你得出什么结论? - greenapps
显示剩余3条评论
2个回答

3
阅读文档并看到错误代码对应的是GEOFENCE_NOT_AVAILABLE,再加上@Mr.Rebot的评论:

“并不是所有智能手表都有支持此项功能所需的硬件[...]"

您可以假定在您的华为手表2设备上不可用地理围栏(GEOFENCE)
您也可以直接向华为支持咨询以获得确认。

1
华为的支持真的很糟糕。 - Vyacheslav

0

这个错误也会在位置未开启时出现。您需要启用位置才能让地理围栏正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接