Android 磨损:地理围栏 - ApiException:1000

Posted

技术标签:

【中文标题】Android 磨损:地理围栏 - ApiException:1000【英文标题】:Android wear: geofence - ApiException: 1000 【发布时间】:2018-06-21 15:58:40 【问题描述】:

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

首先我不确定 Android Wear 是否支持地理围栏? (独立?)我有一个包含 GPS 天线的华为手表 2 LTE,我已经让 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.

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

【问题讨论】:

1000是什么意思? @greenapps 我在这个网站上发现:developers.google.com/android/reference/com/google/android/gms/… 这意味着:GEOFENCE_NOT_AVAILABLE 好的。那么请发表评论。 @greenapps 这只是个例外。没有解决办法? 这不是评论。您应该评论地理围栏不可用的消息。你的结论是什么? 【参考方案1】:

当您阅读文档并看到错误代码对应于GEOFENCE_NOT_AVAILABLE,加上根据@Mr.Rebot 评论:

“并非所有可穿戴设备都具有支持此功能的硬件 [...]”

您可能会认为您的 Huawei watch 2 设备上的GEOFENCE IS NOT AVAILABLE

您也可以直接向to Huawei support询问确认。

【讨论】:

华为支持真的很糟糕【参考方案2】:

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

【讨论】:

以上是关于Android 磨损:地理围栏 - ApiException:1000的主要内容,如果未能解决你的问题,请参考以下文章

Android O 地理围栏触发延迟

Android 地理围栏(多边形)

Android:提高地理围栏的准确性

没有 requestLocationUpdates 不会触发 Android 地理围栏

如何在 Android 中创建地理围栏?

Android:制作地理围栏的简单方法?