我有一个Android应用程序,它使用Awareness API在插入耳机时设置围栏.
我使用代码实现了AwarenessFence,就像以下示例中所示:https://developers.google.com/awareness/android-api/fence-register.
我有一个PendingIntent定义为:
PendingIntent.getBroadcast(context, 0, new Intent("my.application.packageFENCE_RECEIVER_ACTION"), 0)
Run Code Online (Sandbox Code Playgroud)
然后在我的AndroidManifest.xml文件中
<receiver android:name=".fence.FenceDetector$MyFenceReceiver">
<intent-filter>
<action android:name="my.application.packageFENCE_RECEIVER_ACTION" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
这是在Manifest中声明的,因为即使我的应用程序在后台,我也希望接收广播.
这在Android 7.0及更低版本上运行良好,但是当我在Android 8.0上运行时,我收到错误:
BroadcastQueue: Background execution not allowed: receiving Intent { act=my.application.packageFENCE_RECEIVER_ACTION
Run Code Online (Sandbox Code Playgroud)
我认为这是由于Android O上后台执行的新限制.
任何人都可以告诉我如何注册一个广播接收器,它可以在运行API 26的Android设备上在后台监听感知围栏触发器.
让我知道如果有些事情不清楚或者我需要详细说明.
提前致谢
android broadcastreceiver android-broadcastreceiver google-awareness android-8.0-oreo
我正在为我的项目使用Awareness API并通过它接收操作BroadcastReceiver.这是我的代码的问题:链接.问题是除了小米设备外,所有Android版本都能正常运行.凡BroadcastReceiver不接受几个小时后,任何东西.我已经尝试了很多解决方案(比如这个和这个,并为我的应用程序关闭电池节省),但似乎没有任何工作.
我试图通过Awareness API获得fence状态.这是关于此的文档.但......使用时:
Awareness.FenceApi.queryFences
Run Code Online (Sandbox Code Playgroud)
我可以看到它被弃用了,而是需要使用:
Awareness.getFenceClient
Run Code Online (Sandbox Code Playgroud)
任何人都可以举个例子来说明如何使用getFenceClient获取围栏状态?
Awareness.SnapshotApi.getLocation(client)
.setResultCallback(new ResultCallback<LocationResult>() {
@Override
public void onResult(@NonNull LocationResult locationResult) {
if (locationResult.getStatus().isSuccess()) {
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String error = e.getMessage();
}
Run Code Online (Sandbox Code Playgroud)
com.google.android.gms.common.api.ApiException:17:API:ContextManager.API在此设备上不可用。
在Android 9必需手机上运行该应用程序时,我收到此错误作为例外,但在Android 8 Samsung s8上运行时,我也不例外。
我已经更新到最新的16.0.0意识包。
android google-api google-awareness android-8.0-oreo android-9.0-pie
我使用DetectedActivityFence从谷歌API意识.它在我自己的设备上工作正常,但我在Crashlytics中收到了几起关于SecurityException的崩溃事件.
Fatal Exception: java.lang.SecurityException: Invalid API Key for package = [package_name] .Status code received = -1
at android.os.Parcel.readException(Parcel.java:1540)
at android.os.Parcel.readException(Parcel.java:1493)
at com.google.android.gms.common.internal.zzu$zza$zza.zza(Unknown Source)
at com.google.android.gms.common.internal.zzd.zzqz(Unknown Source)
at com.google.android.gms.internal.zzpw$zzc.zzapl(Unknown Source)
at com.google.android.gms.internal.zzpw$zzf.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at com.google.android.gms.internal.zzrn.run(Unknown Source)
at java.lang.Thread.run(Thread.java:818)
Run Code Online (Sandbox Code Playgroud)
这种情况在Android版本之间没有任何相关性,即它影响来自不同制造商和不同Android版本(5.x-6.x)的设备.这些设备没有root,看起来像在Stock ROM上工作.10%的用户会出现此问题.
我确定我使用的是正确的API密钥,否则我可以在自己的设备上看到崩溃,是吗?
有没有人面临同样的问题?
在google awareness API指南页面上,提到了有关上下文类型的内容.
上下文数据包括传感器派生的数据,如位置(lat/lng),地点(家庭,工作,咖啡店)
但是,在带有地方类型地方参考的参考页面上,没有提及住宅类型.有没有办法找出用户是否在家(当然,如果他在谷歌设置中设置)?
我希望在Google Awareness API"fence"触发时启动BroadcastReceiver或IntentService(取决于我的最终处理需要多长时间).例如,也许我想知道在一天中激活一组信标栅栏的次数(假设我随身携带电话).我发现的所有示例都显示了在代码中注册广播接收器,但我的理解是我需要在清单中注册广播接收器,以便操作系统在我的应用程序未运行时向其发送广播.更重要的是,意图ID似乎是一个自定义ID,所以我猜我必须通过代码至少一次在OS上注册它?
我猜我将不得不创建一个或多个测试应用程序来通过反复试验来解决这个问题,但是肯定会感谢任何试过这个并希望分享你的结果的人的回复!
当我开始考虑使用相对较新的Snapshot API 获取天气时,当我无法找到返回结果的任何时间戳时,我有点困惑/担心.
我实现了API的使用,一切似乎都好了.但是,现在我已经Weather在三个截然不同的地方获得了相同的数据.现实生活中的条件和温度确实不一样(另一个设备提供了不同的(正确的)数据).
如果您信任API为您提供没有时间戳或位置的快照数据,或者您真正需要能够信任所返回数据的任何内容.
这是Snapshot API的已知问题吗?有没有办法避免这种情况发生?当天气像这样被卡住时你怎么能恢复?有没有办法知道返回的数据的年龄和/或位置?
我调试了有问题的设备,看到它不是缓存数据的应用程序,并且WeatherResult状态为成功.
我想从背景中获取位置Service.问题是onResult()内部的函数Awareness.SnapshotApi.getLocation(mGoogleApiClient).setResultCallback(new ResultCallback<LocationResult>()没有被调用.这可能有什么问题?
功能onConnected()和onConnectionSuspended()还没有得到所谓的,但是当我打印出来mGoogleApiClient.isConnected() = true返回.
所以我试图理解为什么onConnected()不被调用.
public class BlockingService extends Service implements GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient mGoogleApiClient;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
initAwarenessAPI();
amIClosetoLocation(<LatLng I get from my database>);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
super.onDestroy();
}
private void initAwarenessAPI() {
Context context = getApplicationContext();
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Awareness.API)
.build();
mGoogleApiClient.connect();
}
private …Run Code Online (Sandbox Code Playgroud) 这是我第一次尝试创建android应用程序。在尝试使用Google提供的Awareness API时,我在logcat中收到一个SecurityException,并且出现错误:
“程序包的无效API密钥= com.example.android.project。收到的状态码= 12”。
这意味着,mGoogleApiClient.connect()会导致应用每次崩溃。反正有什么要知道状态码是什么意思?
[BaseServerTask]Server task (PingTask) got error statusCode=403.
com.android.volley.AuthFailureError
at com.android.volley.toolbox.BasicNetwork.performRequest(:com.google.android.gms:32)
at lbf.performRequest(:com.google.android.gms:3)
at com.android.volley.NetworkDispatcher.run(:com.google.android.gms:11)
07-01 15:54:24.303 4839-19074/? E/ctxmgr: [ContextManager3PCredentialsVerifier]Failed ping response: network status=12
07-01 15:54:24.304 4839-19081/? E/AbstractServiceBroker: Getting service failed
java.lang.SecurityException: Invalid API Key for package = com.example.android.project. Status code received = 12
at cpm.a(:com.google.android.gms:19)
at cpp.a(:com.google.android.gms:46)
at ktv.a(:com.google.android.gms:41)
at kwh.onTransact(:com.google.android.gms:8)
at android.os.Binder.transact(Binder.java:507)
at bxh.onTransact(:com.google.android.gms:2)
at android.os.Binder.execTransact(Binder.java:573)
07-01 15:54:24.520 19508-19508/com.example.android.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.project, PID: 19508
java.lang.SecurityException: Invalid API Key for package = …Run Code Online (Sandbox Code Playgroud)