2013-09-10 115 views
2

我將使用google play服務活動識別api編寫應用程序。 android developer site的培訓很簡單,但在過去的幾個小時裏我寫了一個簡單的應用程序,但是我無法從中得到任何結果。 更新:實際上,我將以5秒間隔顯示用戶的當前活動作爲Toa​​st消息(如您​​在ActivityRecognitionService Intent Servie中的OnIntentHandler方法中所見)。我認爲調用Intent是有問題的,因爲你可以在我的代碼中看到說ActivityRecognitionClient在OnConnected方法上連接的吐司。Google活動識別API

我錯過了什麼嗎?

在此先感謝。

清單文件:

<?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"; 
    } 
} 
+0

請準確地描述你想要的事情,以及你的預期。 – chrylis

+0

實際上,我將以5秒間隔顯示用戶的當前活動作爲Toa​​st消息(如您​​在ActivityRecognitionService Intent Servie中的OnIntentHandler方法中所見) – Rightsum

回答

0

您只能從具有處理/尺蠖設置線程調用吐司向上。要實現您的設想,您有兩個選項

  1. 使用IntentService中的廣播並具有一個具有顯示UI的BroadcastReceiver的活動。這樣,當活動不在前臺時,不會顯示吐司。註冊/註銷接收器在你的onResume()/的onPause()
  2. 創建一個處理程序,並調用你的方法在Handler.post(新的Runnable(){..})

Error calling toast from Service Android

1

你可播放的意圖,從服務發送消息給主類
例如,使用您的ActivityRecognitionService.java如下:

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

然後你只需要註冊日e在您的MainActivity.java上爲「myCustomIntentMessage」廣播接收器,並將Toast消息代碼放入廣播接收器OnReceive事件中。