简体   繁体   English

Google Activity Recognition API

[英]Google Activity Recognition API

I am going to write an application using google play services activity recognition api. 我将使用Google Play服务活动识别API编写应用程序。 the training in android developer site was straight forward but in the past couple of hours i wrote a simple application but i can't get any result from it. android开发人员网站中的培训非常简单,但是在过去的几个小时中,我编写了一个简单的应用程序,但无法从中获得任何结果。 UPDATE : Actually i am going to show the current activity of user in 5 second intervals as a toast message (as you can see in OnIntentHandler method in ActivityRecognitionService Intent Servie) . 更新:实际上,我将以5秒的间隔显示用户的当前活动,作为敬酒消息(如您​​在ActivityRecognitionService Intent Servie中的OnIntentHandler方法中所看到的)。 i think something is wrong with the calling the Intent because as you can see in my code the toast that says the ActivityRecognitionClient is connected on OnConnected method. 我认为调用Intent有点问题,因为正如您在我的代码中看到的那样,吐司说ActivityRecognitionClient是在OnConnected方法上连接的。

did i miss something ? 我错过了什么 ?

Thanks in advance. 提前致谢。

Manifest File : 清单文件:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <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" >
        <service
        android:name="com.nikapps.activityrecognition.ActivityRecognitionService"
        android:label="@string/app_name"
        android:exported="false">
        </service>
        <activity
            android:name="com.nikapps.activityrecognition.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>
    </application>

</manifest>

MainActivity.java MainActivity.java

    package com.nikapps.activityrecognition;

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;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener{

    public static int intervals = 5000;
    private PendingIntent pendingIntent;
    private ActivityRecognitionClient activityRecognition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activityRecognition = new ActivityRecognitionClient(this, this, this);
        activityRecognition.connect();
    }

    @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 result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnected(Bundle connectionHint) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(this, ActivityRecognitionService.class);

        pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        activityRecognition.requestActivityUpdates(0, pendingIntent);
    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "disconnected", Toast.LENGTH_SHORT).show();

    }

}

ActivityRecognitionService.java ActivityRecognitionService.java

    package com.nikapps.activityrecognition;

import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;

import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;

public class ActivityRecognitionService extends IntentService{

    public ActivityRecognitionService() {
        super("ActivityRecognitionService");
        Toast.makeText(this, "here", Toast.LENGTH_SHORT).show();
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // TODO Auto-generated method stub

        Toast.makeText(this, "here2", Toast.LENGTH_SHORT).show();
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
        DetectedActivity activity = result.getMostProbableActivity();
        int type = activity.getType();

        Toast.makeText(this, getNameFromType(type), Toast.LENGTH_SHORT).show();

    }

    private String getNameFromType(int activityType) {
        switch(activityType) {
            case DetectedActivity.IN_VEHICLE:
                return "in_vehicle";
            case DetectedActivity.ON_BICYCLE:
                return "on_bicycle";
            case DetectedActivity.ON_FOOT:
                return "on_foot";
            case DetectedActivity.STILL:
                return "still";
            case DetectedActivity.UNKNOWN:
                return "unknown";
            case DetectedActivity.TILTING:
                return "tilting";
        }
        return "unknown";
    }
}

You can broadcast an intent to send messages from the service to the main class 您可以广播将服务中的消息发送到主类的意图
For example, use the following in your ActivityRecognitionService.java: 例如,在ActivityRecognitionService.java中使用以下命令:

Intent mIntent = new Intent("myCustomIntentMessage")
    .putExtra("ActivityType", getNameFromType(type));
getLocalBroadcast().sendBroadcast(mIntent);

Then you just need to register the broadcast receiver for "myCustomIntentMessage" on your MainActivity.java and put the Toast message code in the broadcast receiver OnReceive event. 然后,您只需要在MainActivity.java中为“ myCustomIntentMessage”注册广播接收器,并将Toast消息代码放入广播接收器的OnReceive事件中。

You can only call the Toast from a thread that has a handler/Looper set up. 您只能从已设置处理程序/ Looper的线程中调用Toast。 To achieve what you're envisioning you have two options 为了实现您的预​​期,您有两种选择

  1. Use a broadcast from the IntentService and have an activity that has a BroadcastReceiver that shows the UI. 使用来自IntentService的广播,并进行一个具有显示UI的BroadcastReceiver的活动。 This way, the toast is not shown when the activity is not in the foreground . 这样,当活动不在前台时就不会显示吐司。 register/unregister the receiver in your onResume()/onPause() 在onResume()/ onPause()中注册/注销接收者
  2. create a handler and call your method in Handler.post(new Runnable() {.. }) 创建一个处理程序并在Handler.post(new Runnable(){..})中调用您的方法

Error calling toast from Service Android 从服务Android调用Toast时出错

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM