Android 感知API显著延迟了活动回调
我已经创建了一个类和一个BroadcastReceiver,以便在步行或跑步结束时从感知api获取回调。 我没有收到及时的回电,起初以为是因为我注册了一个“停止”的回电,但在我的手机关机一段时间后,我确实收到了几次回电!但这远远不是我停止走路的时候。停车后至少5分钟。 有时,即使Google Fit应用程序记录活动,我也不会收到回调 因为我至少收到过几次回电,我知道注册是可以的。为什么这么晚才接到电话,有时还没接到 作为后台参考,我正在主activty的onStart中注册这些回调,即在活动的onStart中调用InitiateAware。我从不注销它们。 我不打算在生产中这样使用它,它只是为了测试。另外,我最初尝试在应用程序上下文中注册fences失败 下面是我创建的帮助器类,用于设置google客户端和fences的注册Android 感知API显著延迟了活动回调,android,google-fit,google-awareness,Android,Google Fit,Google Awareness,我已经创建了一个类和一个BroadcastReceiver,以便在步行或跑步结束时从感知api获取回调。 我没有收到及时的回电,起初以为是因为我注册了一个“停止”的回电,但在我的手机关机一段时间后,我确实收到了几次回电!但这远远不是我停止走路的时候。停车后至少5分钟。 有时,即使Google Fit应用程序记录活动,我也不会收到回调 因为我至少收到过几次回电,我知道注册是可以的。为什么这么晚才接到电话,有时还没接到 作为后台参考,我正在主activty的onStart中注册这些回调,即在活动的o
public class AwarenessHelper {
public static final String WALKING_ENDED_FENCE = "walkingEndedKey";
public static final String RUNNING_ENDED_FENCE = "runningEndedKey";
public static final String TYPE_2_WALKING = "duringWalkingKey";
public static final String TYPE_2_RUNNING = "duringRunningKey";
private String tag = AwarenessHelper.class.getSimpleName();
public void initiateAwareness(final Activity context)
{
final GoogleApiClient googleApiClient = buildClient(context);
Log.d(tag, "Initiating blocking connect");
googleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
if ( googleApiClient.isConnected() )
{
Log.d(tag, "Client connected, initiating awareness fence registration");
registerAwarenessFences(context, googleApiClient);
}
else
{
Log.d(tag, "Couldn't connect");
}
}
@Override
public void onConnectionSuspended(int i) {
}
});
googleApiClient.connect();
}
private void registerAwarenessFences(Context context, GoogleApiClient mGoogleApiClient) {
Awareness.FenceApi.updateFences(
mGoogleApiClient,
new FenceUpdateRequest.Builder()
.addFence(WALKING_ENDED_FENCE, DetectedActivityFence.stopping(DetectedActivityFence.WALKING), getBroadcastPendingIntent(context))
.addFence(RUNNING_ENDED_FENCE, DetectedActivityFence.stopping(DetectedActivityFence.RUNNING), getBroadcastPendingIntent(context))
.addFence(TYPE_2_WALKING, DetectedActivityFence.during(DetectedActivityFence.WALKING), getBroadcastPendingIntent(context))
.addFence(TYPE_2_RUNNING, DetectedActivityFence.stopping(DetectedActivityFence.RUNNING), getBroadcastPendingIntent(context))
.build())
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.i(tag, "Fence was successfully registered.");
} else {
Log.e(tag, "Fence could not be registered: " + status);
}
}
});
}
private GoogleApiClient buildClient(final Activity activity)
{
GoogleApiClient client = new GoogleApiClient.Builder(activity)
.addApi(Awareness.API)
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if ( connectionResult.hasResolution() && connectionResult.getErrorCode() == CommonStatusCodes.SIGN_IN_REQUIRED )
{
try {
connectionResult.startResolutionForResult(activity, GOOGLE_FIT_AUTHORIZATION_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}
})
.build();
return client;
}
private PendingIntent getBroadcastPendingIntent(Context context)
{
Intent intent = new Intent(AWARENESS_BROADCAST_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
return pendingIntent;
}
}
public类AwarenessHelper{
公共静态最终字符串漫游\u结束\u FENCE=“WalkingedKey”;
公共静态最终串运行\u结束\u FENCE=“runningEndedKey”;
公共静态最终字符串类型_2_WALKING=“duringWalkingKey”;
公共静态最终字符串类型_2_RUNNING=“duringRunningKey”;
私有字符串标记=AwarenessHelper.class.getSimpleName();
公共无效初始意识(最终活动上下文)
{
最终GoogleAppClient GoogleAppClient=buildClient(上下文);
日志d(标签“启动阻塞连接”);
注册连接回调(新的GoogleAppClient.ConnectionCallbacks(){
@凌驾
未连接的公共无效(@Nullable Bundle){
if(googleApiClient.isConnected())
{
Log.d(标签,“连接客户端,启动感知围栏注册”);
RegisterWarenessFunces(上下文、GoogleAppClient);
}
其他的
{
Log.d(标记“无法连接”);
}
}
@凌驾
公共空间连接暂停(int i){
}
});
googleApiClient.connect();
}
专用无效注册表WarenessFences(上下文上下文,GoogleAppClient MgoogleAppClient){
Awareness.FenceApi.updateFences(
MGoogleapClient,
新建FenceUpdateRequest.Builder()
.addFence(行走\结束\围栏,检测到的活动围栏。停止(检测到的活动围栏。行走),GetBroadcastingPendingEvent(上下文))
.addFence(正在运行\u结束\u FENCE,检测到的活动FENCE.stopping(检测到的活动FENCE.RUNNING),GetBroadcastPendingEvent(上下文))
.addFence(类型2_WALKING,DetectedActivityFence.during(DetectedActivityFence.WALKING),GetBroadcastingPendingEvent(上下文))
.addFence(类型为运行,检测到的活动fence.stopping(检测到的活动fence.RUNNING),GetBroadcastPendingEvent(上下文))
.build())
.setResultCallback(新的ResultCallback(){
@凌驾
public void onResult(@NonNull状态){
if(status.issucess()){
Log.i(标记“围栏已成功注册”);
}否则{
Log.e(标签“围栏无法注册:+状态”);
}
}
});
}
专用GoogleapClient buildClient(最终活动)
{
GoogleAppClient=新的GoogleAppClient.Builder(活动)
.addApi(Awareness.API)
.addOnConnectionFailedListener(新的GoogleAppClient.OnConnectionFailedListener(){
@凌驾
public void onconnection失败(@NonNull ConnectionResult ConnectionResult){
if(connectionResult.hasResolution()&&connectionResult.getErrorCode()==CommonStatusCodes.SIGN\u IN\u必需)
{
试一试{
connectionResult.StartResult解决方案(活动、GOOGLE\u FIT\u授权\u请求\u代码);
}catch(IntentSender.sendtintentexe){
e、 printStackTrace();
}
}
}
})
.build();
返回客户;
}
私有PendingEvent GetBroadcastPendingEvent(上下文)
{
意向意向=新意向(意识、广播、行动);
PendingEvent PendingEvent=PendingEvent.getBroadcast(上下文,0,意图,0);
返回悬挂式帐篷;
}
}
这是广播接收机:
public class AwarenessHelper {
public static final String WALKING_ENDED_FENCE = "walkingEndedKey";
public static final String RUNNING_ENDED_FENCE = "runningEndedKey";
public static final String TYPE_2_WALKING = "duringWalkingKey";
public static final String TYPE_2_RUNNING = "duringRunningKey";
private String tag = AwarenessHelper.class.getSimpleName();
public void initiateAwareness(final Activity context)
{
final GoogleApiClient googleApiClient = buildClient(context);
Log.d(tag, "Initiating blocking connect");
googleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
if ( googleApiClient.isConnected() )
{
Log.d(tag, "Client connected, initiating awareness fence registration");
registerAwarenessFences(context, googleApiClient);
}
else
{
Log.d(tag, "Couldn't connect");
}
}
@Override
public void onConnectionSuspended(int i) {
}
});
googleApiClient.connect();
}
private void registerAwarenessFences(Context context, GoogleApiClient mGoogleApiClient) {
Awareness.FenceApi.updateFences(
mGoogleApiClient,
new FenceUpdateRequest.Builder()
.addFence(WALKING_ENDED_FENCE, DetectedActivityFence.stopping(DetectedActivityFence.WALKING), getBroadcastPendingIntent(context))
.addFence(RUNNING_ENDED_FENCE, DetectedActivityFence.stopping(DetectedActivityFence.RUNNING), getBroadcastPendingIntent(context))
.addFence(TYPE_2_WALKING, DetectedActivityFence.during(DetectedActivityFence.WALKING), getBroadcastPendingIntent(context))
.addFence(TYPE_2_RUNNING, DetectedActivityFence.stopping(DetectedActivityFence.RUNNING), getBroadcastPendingIntent(context))
.build())
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.i(tag, "Fence was successfully registered.");
} else {
Log.e(tag, "Fence could not be registered: " + status);
}
}
});
}
private GoogleApiClient buildClient(final Activity activity)
{
GoogleApiClient client = new GoogleApiClient.Builder(activity)
.addApi(Awareness.API)
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if ( connectionResult.hasResolution() && connectionResult.getErrorCode() == CommonStatusCodes.SIGN_IN_REQUIRED )
{
try {
connectionResult.startResolutionForResult(activity, GOOGLE_FIT_AUTHORIZATION_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}
})
.build();
return client;
}
private PendingIntent getBroadcastPendingIntent(Context context)
{
Intent intent = new Intent(AWARENESS_BROADCAST_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
return pendingIntent;
}
}
public类AwarenessHelper{
公共静态最终字符串漫游\u结束\u FENCE=“WalkingedKey”;
公共静态最终串运行\u结束\u FENCE=“runningEndedKey”;
公共静态最终字符串类型_2_WALKING=“duringWalkingKey”;
公共静态最终字符串类型_2_RUNNING=“duringRunningKey”;
私有字符串标记=AwarenessHelper.class.getSimpleName();
公共无效初始意识(最终活动上下文)
{
最终GoogleAppClient GoogleAppClient=buildClient(上下文);
日志d(标签“启动阻塞连接”);
注册连接回调(新的GoogleAppClient.ConnectionCallbacks(){
@凌驾
未连接的公共无效(@Nullable Bundle){
if(googleApiClient.isConnected())
{
Log.d(标签,“连接客户端,启动感知围栏注册”);
RegisterWarenessFunces(上下文、GoogleAppClient);
}
其他的
{
Log.d(标记“无法连接”);
}
}
@凌驾
公共空间连接暂停(int i){
}
});