首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android活动识别与Nexus 5不兼容

Android活动识别与Nexus 5不兼容
EN

Stack Overflow用户
提问于 2014-03-12 12:50:21
回答 3查看 5.5K关注 0票数 1

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

我从教程中构建了一个最小的示例,但也没有使用我的Nexus 5设备进行任何活动更新。根据我的HTC愿望(基于Android2.3.7的MildWild 5.0 ),它运行得非常好。我怀疑Google服务,但这两款手机都安装了4.2.43版本

MainActivity:

代码语言:javascript
复制
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");
    }

}

清单:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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找到了正确的问题。当设备仍然有很高的信心时,它根本不会发出识别的意图。这在一年前是完全不同的(在其他设备上)。为了检测设备是否仍然存在,我用它的时间戳记录最后一个传入的活动,并定期检查。一个旧的时间戳标志着这个设备在那个时候仍然存在。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-04-02 06:53:32

我建议您增加检测间隔,传感器至少需要6到7秒才能检测到活动中的变化。此外,我观察到,如果设备仍然存在一段时间,ActivityRecognition就会停止发射。当设备再次启动时,它们立即被触发。

票数 1
EN

Stack Overflow用户

发布于 2014-10-30 02:08:15

也许你在Nexus 5设备上禁用了定位服务..。

活动识别需要激活/启用位置服务。

票数 0
EN

Stack Overflow用户

发布于 2015-05-27 16:34:29

正如canopi所建议的,增加检测‘时间’,其中0会给你最快的结果。

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

代码语言:javascript
复制
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(ActivityRecognition.API)
            .build();
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22352134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档