2014-11-21 96 views

回答

10

您可以爲此使用GooglePlayServices。

它爲ActivityRecognition提供了特殊的API,它爲每個用戶返回了用戶活動的置信度。

https://developer.android.com/reference/com/google/android/gms/location/ActivityRecognitionClient.html

http://developer.android.com/training/location/activity-recognition.html

+0

聽起來不錯。是否有最低的API級別要求,或者只有play服務庫才能完成工作? – 2014-11-21 10:43:26

+0

@MayurMore它支持從android 2.2開始...... – 2014-11-21 12:12:26

+0

這兩個鏈接現在指向其他東西 – 2015-07-23 16:07:12

13

這個問題是很老,但因爲有新的技術都在那裏,我認爲這是值得一提的是,如果有人仍然遇到此問題。

我能想出3個選項:

  1. 您可以實現自己的檢測步行,駕車,騎車技巧 - 用Activity recognitionreceiving location updates,雖然我建議不要這樣做,不重新發明輪子,已經有很好的發展,現在是2016年。
  2. 您可以使用免費的sdk Neura,當您的用戶開始/完成駕駛,開始/結束步行,開始/結束運行,read more of the events you can get from Neura時,它可以向您發送活動。

    檢查了這個git project:基本上,該項目有Nuera可以檢測到的所有事件。只需參加這個項目並使其成爲您自己的項目非常簡單。

    我強烈推薦使用這個Neura sdk選項。

  3. 您可以使用谷歌的FenceApi爲了宣佈柵欄。例如,這是用於檢測駕駛圍欄的代碼。

    儘管這種方法看起來不錯,但我已經面對這樣一個事實,即當事件發生時,這個api並沒有告訴我,有時候我開始走路/跑步時花了很長時間,當api告訴我那個事件。

    a。包括依賴於你的應用程序的文件的build.gradle:

    compile 'com.google.android.gms:play-services-location:+' 
    
        compile 'com.google.android.gms:play-services-contextmanager:+' 
    

    灣清單定義:

    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    
    <application 
        android:allowBackup="true" 
        android:icon="@mipmap/ic_launcher" 
        android:label="@string/app_name" 
        android:supportsRtl="true" 
        android:theme="@style/AppTheme" > 
    
        <meta-data 
         android:name="com.google.android.awareness.API_KEY" 
         android:value="PUT_YOUR_AWARENESS_KEY_HERE" /> 
    
        <activity android:name=".MainActivity" > 
         <intent-filter> 
          <action android:name="android.intent.action.MAIN" /> 
    
          <category android:name="android.intent.category.LAUNCHER" /> 
         </intent-filter> 
        </activity> 
    </application> 
    

    PUT_YOUR_AWARENESS_KEY_HERE:你需要生成一個密鑰here

    c。你MainActivity類別 - 解釋連接到代碼:

    public class MainActivity extends Activity { 
    
        private GoogleApiClient mGoogleApiClient; 
        private PendingIntent mPendingIntent; 
        private FenceReceiver mFenceReceiver; 
    
        // The intent action which will be fired when your fence is triggered. 
        private final String FENCE_RECEIVER_ACTION = BuildConfig.APPLICATION_ID + "FENCE_RECEIVER_ACTION"; 
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_main); 
         mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Awareness.API).build(); 
         mGoogleApiClient.connect(); 
         // Set up the PendingIntent that will be fired when the fence is triggered. 
         mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(FENCE_RECEIVER_ACTION), 0); 
         // The broadcast receiver that will receive intents when a fence is triggered. 
         mFenceReceiver = new FenceReceiver(); 
         registerReceiver(mFenceReceiver, new IntentFilter(FENCE_RECEIVER_ACTION)); 
         createFence(DetectedActivityFence.IN_VEHICLE, "InVehicleFence"); 
        } 
    
        @Override 
        public void onDestroy() { 
         try { 
          unregisterReceiver(mFenceReceiver); //Don't forget to unregister the receiver 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
         super.onDestroy(); 
        } 
    
        private void createFence(int detectedActivityFence, final String fenceKey) { 
         AwarenessFence fence = DetectedActivityFence.during(detectedActivityFence); 
         // Register the fence to receive callbacks. 
         Awareness.FenceApi.updateFences(
           mGoogleApiClient, new FenceUpdateRequest.Builder().addFence(fenceKey, fence, mPendingIntent) 
             .build()).setResultCallback(new ResultCallback<Status>() { 
          @Override 
          public void onResult(@NonNull Status status) { 
           if (status.isSuccess()) { 
            Log.i(getClass().getSimpleName(), "Successfully registered."); 
           } else { 
            Log.e(getClass().getSimpleName(), "Could not be registered: " + status); 
           } 
          } 
         }); 
        } 
    
        // Handle the callback on the Intent. 
        public class FenceReceiver extends BroadcastReceiver { 
         @Override 
         public void onReceive(Context context, Intent intent) { 
          FenceState fenceState = FenceState.extract(intent); 
          switch (fenceState.getCurrentState()) { 
           case FenceState.TRUE: 
            Log.i(fenceState.getFenceKey(), "Active"); 
            break; 
           case FenceState.FALSE: 
            Log.i(fenceState.getFenceKey(), "Not Active"); 
            break; 
          } 
         } 
        } 
    } 
    

    此示例是僅用於檢測行駛狀態,但是,你可以叫「createFence」與其他活性的方法,如:

    createFence(DetectedActivityFence.TILTING, "TiltingFence"); 
    createFence(DetectedActivityFence.WALKING, "WalkingFence"); 
    createFence(DetectedActivityFence.ON_FOOT, "OnFootFence"); 
    createFence(DetectedActivityFence.RUNNING, "RunningFence");