gpt4 book ai didi

安卓磨损 : geofence - ApiException: 1000

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:39 26 4
gpt4 key购买 nike

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

首先,我不确定 Android Wear 是否支持地理围栏? (独立?)我有一个包含 GPS 天线的 Huawei watch 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.

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

最佳答案

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

"Not all wear devices have the hardware to support this [...]"

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

你也可以问to Huawei support直接确认。

关于安卓磨损 : geofence - ApiException: 1000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48222022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com