Android的行为识别不工作的Nexus 5 [英] Android Activity Recognition not working with Nexus 5

查看:306
本文介绍了Android的行为识别不工作的Nexus 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用谷歌的行为识别更新一个code工作。现在,突然之间,这些看起来虽然要求每20秒发送更新或者每秒数次,时不再来。我并没有改变的code和检查早期版本,但得到了同样的问题。

I had a code working that was using Google's Activity Recognition Updates. Now all of a sudden these seem to send updates either several times per second or never although requested every 20 seconds. I haven't changed the code and checked earlier versions but got the same problem.

我建的教程一个最小的例子,但也让我的Nexus 5的设备没有活动的更新。随着我的HTC Desire(MildWild 5.0基于Android 2.3.7),它完美的罚款。我怀疑谷歌播放服务,但是这两款手机都有4.2.43版本的安装

I built a minimal example from the tutorial but also get no activity updates with my Nexus 5 device. With my HTC Desire (MildWild 5.0 based on Android 2.3.7) it works perfectly fine. I suspected Google Play Services but both phones have version 4.2.43 installed

MainActivity:

MainActivity:

package com.example.testactivities;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.ActivityRecognitionClient;

public class MainActivity extends FragmentActivity implements
ConnectionCallbacks, OnConnectionFailedListener {
    // Constants that define the activity detection interval
    public static final int MILLISECONDS_PER_SECOND = 1000;
    public static final int DETECTION_INTERVAL_SECONDS = 1;
    public static final int DETECTION_INTERVAL_MILLISECONDS = 
            MILLISECONDS_PER_SECOND * DETECTION_INTERVAL_SECONDS;
    /*
     * Store the PendingIntent used to send activity recognition events
     * back to the app
     */
    private PendingIntent mActivityRecognitionPendingIntent;
    // Store the current activity recognition client
    private ActivityRecognitionClient mActivityRecognitionClient;
    // Flag that indicates if a request is underway.
    private boolean mInProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
         * Instantiate a new activity recognition client. Since the
         * parent Activity implements the connection listener and
         * connection failure listener, the constructor uses "this"
         * to specify the values of those parameters.
         */
        mActivityRecognitionClient =
                new ActivityRecognitionClient(this, this, this);
        /*
         * Create the PendingIntent that Location Services uses
         * to send activity recognition updates back to this app.
         */
        Intent intent = new Intent(
                this.getApplicationContext(), ActivityRecognitionIntentService.class);
        /*
         * Return a PendingIntent that starts the IntentService.
         */
        mActivityRecognitionPendingIntent =
                PendingIntent.getService(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        // Start with the request flag set to false
        mInProgress = false;
        this.startUpdates();
    }

    /**
     * Request activity recognition updates based on the current
     * detection interval.
     *
     */
     public void startUpdates() {
        // If a request is not already underway
        if (!mInProgress) {
            // Indicate that a request is in progress
            mInProgress = true;
            // Request a connection to Location Services
            mActivityRecognitionClient.connect();
        //
        } else {
            /*
             * A request is already underway. You can handle
             * this situation by disconnecting the client,
             * re-setting the flag, and then re-trying the
             * request.
             */
        }
    }

     /*
      * Called by Location Services once the location client is connected.
      *
      * Continue by requesting activity updates.
      */
     @Override
     public void onConnected(Bundle dataBundle) {
         Log.v("EXAMPLE","connected");
         /*
          * Request activity recognition updates using the preset
          * detection interval and PendingIntent. This call is
          * synchronous.
          */
         mActivityRecognitionClient.requestActivityUpdates(
                 DETECTION_INTERVAL_MILLISECONDS,
                 mActivityRecognitionPendingIntent);
         /*
          * Since the preceding call is synchronous, turn off the
          * in progress flag and disconnect the client
          */
         mInProgress = false;
         mActivityRecognitionClient.disconnect();
     }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        Log.v("EXAMPLE","Connection failed");

    }

    @Override
    public void onDisconnected() {
        Log.v("EXAMPLE","Disconnected");
    }

}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testactivities"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="19" />
    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity
            android:name="com.example.testactivities.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name="com.example.testactivities.ActivityRecognitionIntentService"/>
    </application>

</manifest>

RecognitionIntentService:

RecognitionIntentService:

package com.example.testactivities;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class ActivityRecognitionIntentService extends IntentService {

    public ActivityRecognitionIntentService() {
        super("ActivityRecognitionIntent");
        Log.v("EXAMPLE","constructor");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v("EXAMPLE","new activity update");
    }

}

更新:我相信canopi确定正确的问题。当设备仍具有较高的信心,它不火的认可意图可言。这是绝对不同的一年前(在其他设备上)。为了检测该设备是否仍在我日志最后进入的活性,其时间戳和定期检查的。该设备已经仍然在该时间的老时间戳信号

UPDATE: I believe canopi identified the correct problem. When the device is still with high confidence, it doesn't fire recognition intents at all. This was definitely different a year ago (on other devices). To detect if the device is still I log the last incoming activity with its timestamp and check periodically for that. An old timestamp signals that the device has been still for that time.

推荐答案

我会建议你提高了检测的时间间隔,至少需要6到7秒的传感器来检测你的活动的变化。另外,我还发现,ActivityRecognition停止射击的意图,如果该设备仍是在一定的时间段。它们被立即触发时,该设备处于运动再次

I would suggest you increase the detection interval, it takes at least 6 to 7 seconds for the sensors to detect a change in your activity. Also, I have observed that ActivityRecognition stops firing intents if the device is Still for a certain period of time. They are immediately triggered when the device is in motion again.

这篇关于Android的行为识别不工作的Nexus 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆