Android穿:geofence - ApiException:1000

时间:2018-01-12 08:09:24

标签: android gps wear-os android-geofence

我正在为 Android Wear 构建Android应用。为节省电池,我试图使用Geofences来跟踪您是否进入或退出某个位置。但我无法让它发挥作用。

首先,我不确定Android Wear是否支持Geofences? (独立?)我有一个华为手表2 LTE,其中包含一个GPS天线,我已经有了FusedLocationClient工作,所以我知道GPS工作。我还有我的地理围栏代码正在工作手机没问题。

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

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

我在Google API documentation上发现这意味着: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!");
    }
}

Here is also a github link to the example I wrote.

希望有人可以帮我解决这个问题,因为我已经尝试了所有我知道的事情:)

2 个答案:

答案 0 :(得分:1)

在您阅读文档时,看到错误代码与GEOFENCE_NOT_AVAILABLE相对应,另外根据@ Mr.Rebot评论:

  

“并非所有磨损设备都有硬件支持这个[...]”

您可能认为您的Huawei watch 2设备 GEOFENCE不可用

您也可以直接要求to Huawei support进行确认。

答案 1 :(得分:0)

未打开位置信息时也会出现此错误。您需要启用位置才能启用地理围栏。