Android wear:geofence-ApiException:1000

Android wear:geofence-ApiException:1000,android,gps,wear-os,android-geofence,Android,Gps,Wear Os,Android Geofence,我正在为Android Wear构建一个Android应用程序。为了节省电池,我尝试使用Geofences来跟踪您是否进入或退出某个位置。但是我不能让它工作 首先,我不确定Android Wear是否支持GeoFence?(独立?我有一个华为watch 2 LTE,其中包含一个GPS天线,我已经让FusedLocationClient工作了,所以我知道GPS工作。我还让我的地理围栏代码在手机上工作,没有任何问题 当我运行代码时,会出现以下异常: com.google.android.gms.co

我正在为Android Wear构建一个Android应用程序。为了节省电池,我尝试使用Geofences来跟踪您是否进入或退出某个位置。但是我不能让它工作

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

当我运行代码时,会出现以下异常:

com.google.android.gms.common.api.ApiException: 1000:
我在网上发现这意味着:
geofiness\u不可用
,这没有给我额外的信息

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

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!");
    }
}
公共类GeofencingService扩展服务实现OnSuccessListener、OnFailureListener{
私有静态最终字符串TAG=“GeofencingService”;
私有静态最终整数通知_ID=1;
私人Geofencing客户管理Geofencing客户;
私人名单;
私人通知经理通知经理;
@可空
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
public void onCreate(){
super.onCreate();
mNotificationManager=(NotificationManager)getSystemService(通知服务);
mGeofencingClient=LocationServices.getGeofencingClient(this);
mGeofenceList=newarraylist();
createGeofences();
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
showNofification();
设置土工围栏();
返回super.onStartCommand(intent、flags、startId);
}
@凌驾
公共空间{
super.ondestory();
mNotificationManager.cancel(通知ID);
}
私有PendingEvent GetGeoFencePendingEvent()
{
Intent-Intent=新的Intent(这个,geofenginebroadcastReceiver.class);
返回pendingent.getBroadcast(this,0,intent,pendingent.FLAG\u UPDATE\u CURRENT);
}
私人GeofencingRequest getGeofencingRequest(){
GeofencingRequest.Builder=新的GeofencingRequest.Builder();
//初始_TRIGGER_ENTER标志表示地理围栏服务应触发
//GEOFENCE_TRANSITION_添加GEOFENCE时以及设备
//已经在土工栅栏里了。
builder.setInitialTrigger(初始触发输入初始触发退出);
//添加要由地理围栏服务监控的地理围栏。
建造商。添加地理围栏(MGEOFENCESLIST);
//返回GeofencingRequest。
返回builder.build();
}
私人空间设置GeoFence()
{
试一试{
Log.i(标记“设置地理围栏…”);
mGeofencingClient.addgeofenses(getGeofencingRequest(),getgeofencependingent()).addOnSuccessListener(this.addOnFailureListener(this);
}catch(SecurityException-ex)
{
Log.d(标记,“异常:+ex.getMessage());
}
}
私有void createGeofences()
{
Log.i(标记“创建地理围栏…”);
mGeofenceList.add(new geofinence.Builder()
//设置地理围栏的请求ID。这是一个用于标识此项的字符串
//地球围栏。
.setRequestId(“Test1”)
//设置此地理围栏的圆形区域。
.setCircularRegion(
50.03535,
4.33139,
100
)
.setExpirationDuration(永不过期)
//设置感兴趣的转换类型。仅为这些类型生成警报
//转换。我们在这个示例中跟踪进入和退出转换。
.setTransitionTypes(地理围栏(过渡)输入|地理围栏(过渡)退出)
//创建地理围栏。
.build());
}
私有无效显示无效()
{
意向appIntent=新意向(此,MainActivity.class);
PendingEvent contentIntent=PendingEvent.getActivity(this,0,appIntent,0);
Notification Notification=新建NotificationCompat.Builder(此NotificationCompat.CATEGORY_服务)
.setSmallIcon(R.mipmap.ic_启动器)
.setContentTitle(getText(R.string.location\u service\u title))
.setContentText(getText(R.string.location\u service\u text))
.setLocalOnly(true)
.正在进行(正确)
.setContentIntent(contentIntent)
.build();
mNotificationManager.notify(通知ID,通知);
}
@凌驾
public void onFailure(@NonNull异常e){
Log.d(标记,“异常:+e.getMessage());
new Handler().postDelayed(new Runnable()){
@凌驾
公开募捐{
设置土工围栏();
}
}, 2000);
}
@凌驾
成功时的公共无效(对象o){
Log.d(标记“Success!”);
}
}


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

当您阅读文档时,看到错误代码对应于
Geopfence\u NOT\u AVAILABLE
,另外根据@Mr.Rebot注释:

“并非所有磨损设备都具有支持此功能的硬件[…]”

您可以假设您的华为watch 2设备上没有地理围栏


您也可以直接要求确认。

当位置未打开时,也会出现此错误。您需要启用位置以使geofence正常工作。

1000是什么意思?@greenapps正如我在本网站上发现的:它的意思是:geofence\u NOT\u AVAILABLEOk。那么请评论这只是个例外。没有解决方案?这不是评论。您应该对geofence