【问题标题】:Android Activity Recognition not working with Nexus 5Android 活动识别不适用于 Nexus 5
【发布时间】:2014-03-12 12:50:21
【问题描述】:

我的代码正在使用 Google 的活动识别更新。现在突然之间,这些似乎每秒发送几次或从未发送更新,尽管每 20 秒请求一次。我没有更改代码并检查过早期版本,但遇到了同样的问题。

我根据教程构建了一个最小示例,但我的 Nexus 5 设备也没有任何活动更新。使用我的 HTC Desire(基于 Android 2.3.7 的 MildWild 5.0)它工作得非常好。我怀疑 Google Play 服务,但两部手机都安装了 4.2.43 版

主活动:

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>

识别意图服务:

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 发现了正确的问题。当设备仍然具有很高的置信度时,它根本不会触发识别意图。一年前(在其他设备上)这绝对是不同的。为了检测设备是否仍然存在,我使用时间戳记录最后传入的活动并定期检查。旧的时间戳表明该设备在那段时间内一直处于静止状态。

【问题讨论】:

  • 遇到完全相同的问题。我注意到的是,在我在 ActivityRecognitionClient 上调用断开连接后,它永远不会触发 onDisconnected 回调。这似乎是罪魁祸首,因为我让这个工作了一个小时左右,并且在断开连接完成后立即开始更新。
  • 很高兴听到有人遇到同样的问题。我将检查 onDisconnected 回调。我认为那也没有触发。

标签: android google-play-services activity-recognition


【解决方案1】:

我建议您增加检测间隔,传感器至少需要 6 到 7 秒才能检测到您的活动变化。另外,我观察到如果设备静止一段时间,ActivityRecognition 会停止触发意图。当设备再次移动时,它们会立即触发。

【讨论】:

  • 它应该在活动发生变化时触发,以便在长时间静止不动时停止触发并不让我担心。检测间隔绝对不是问题。
【解决方案2】:

也许您在 Nexus 5 设备上禁用了定位服务...
活动识别需要激活/启用位置服务。

【讨论】:

  • 感谢您的回答,尽管线程很旧。我更新了我的问题并将 canopi 的答案标记为正确。
【解决方案3】:

根据 canopi 的建议,增加检测“时间”,其中 0 将为您提供最快的结果。

我遇到了同样的问题,我没有直接在 onCreate 上添加 build() 部分,而是添加了下面的方法并在 onCreate() 上调用它。尝试从这个google sample 追踪代码。我从那里删除了代码。

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(ActivityRecognition.API)
            .build();
}

还将buildGoogleApiClient() 添加到您的 onCreate 方法中。

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多