2

我将使用 google play services 活动识别 api 编写一个应用程序。android 开发者网站的培训很简单,但在过去的几个小时里,我写了一个简单的应用程序,但我无法从中得到任何结果。更新:实际上,我将以 5 秒的间隔将用户的当前活动显示为 toast 消息(如您​​在 ActivityRecognitionService Intent Servie 中的 OnIntentHandler 方法中所见)。我认为调用 Intent 有问题,因为正如您在我的代码中看到的,表示 ActivityRecognitionClient 已在 OnConnected 方法上连接的 toast。

我错过了什么 ?

提前致谢。

清单文件:

    <?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

    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

    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";
    }
}
4

2 回答 2

1

您可以广播意图以将消息从服​​务发送到主类
。例如,在 ActivityRecognitionService.java 中使用以下内容:

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

然后,您只需在 MainActivity.java 上为“myCustomIntentMessage”注册广播接收器,并将 Toast 消息代码放入广播接收器 OnReceive 事件中。

于 2014-10-30T01:54:40.940 回答
0

您只能从设置了处理程序/循环器的线程调用 Toast。为了实现您的设想,您有两种选择

  1. 使用来自 IntentService 的广播,并拥有一个具有显示 UI 的 BroadcastReceiver 的活动。这样,当活动不在前台时,吐司不会显示。在您的 onResume()/onPause() 中注册/取消注册接收器
  2. 创建一个处理程序并在 Handler.post(new Runnable() {.. }) 中调用您的方法

从 Service Android 调用 toast 时出错

于 2013-10-07T21:02:29.170 回答