2015-01-09 23 views
1
用戶的當前活動

我與來自http://developer.android.com/training/location/activity-recognition.html認識到Android的

識別用戶的當前活動的工作,我用便接踵而來代碼來創建新的ActivityRecognitionClient:

public class GPSLocationService extends Service implements ConnectionCallbacks, OnConnectionFailedListener { 

private String TAG = "[ServiceDetect]"; 
// 
private ActivityRecognitionClient mActivityRecognitionClient ; 
private PendingIntent mPendingIntent ; 


@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 


@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // TODO Auto-generated method stub 




      if (checkGooglePlayAvaible()) { 
       startTrack(); 
      } 



    return START_STICKY; 
} 

@Override 
public void onConnected(Bundle bundle) { 
    // TODO Auto-generated method stub 
    // code detetc user acitivity here  
      getActivityRecognitionClient().requestActivityUpdates((2 * 60 * 1000), createPendingRequest()); 

} 

@Override 
public void onDisconnected() { 
    // TODO Auto-generated method stub 


    mActivityRecognitionClient = null; 
    mPendingIntent.cancel(); 
    mPendingIntent = null; 

} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    if (connectionResult.hasResolution()) { 

     try{ 
      connectionResult.startResolutionForResult((Activity) this.getApplicationContext(), 0); 

     } catch (IntentSender.SendIntentException e) { 
      // it happens if the resolution intent has been canceled, 
      // or is no longer able to execute the request.e 
      e.printStackTrace(); 
     } 
    } else { 
     // Google Play services has no idea how to fix the issue 
     // it rarely happens for the location service 
    } 
} 

public void startTrack() { 
    try { 

     if (!getActivityRecognitionClient().isConnected() || !getActivityRecognitionClient().isConnecting()) { 

       Log.v(TAG, "getActivityRecognitionClient is not connected");      
       getActivityRecognitionClient().connect();    

     } 

} 

public PendingIntent createPendingRequest() { 
    if (null != mPendingIntent) { 

    } else { 
     Intent intent = new Intent(getApplicationContext(), ServiceFour.class); 
     mPendingIntent = PendingIntent.getService(getApplicationContext(), 2, intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
    } 
    return mPendingIntent; 
} 

/** 
* check googleplayservices is avaible or not 
* 
* @return true if is avaible flase if not 
*/ 
public boolean checkGooglePlayAvaible() { 

    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) { 
     return true; 
    } 
    return false; 
} 

@Override 
public void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    mIsRemove = false; 
} 

private ActivityRecognitionClient getActivityRecognitionClient() { 
    if (mActivityRecognitionClient == null) { 

     mActivityRecognitionClient = new ActivityRecognitionClient(getApplicationContext(), this, this); 
    } 
    return mActivityRecognitionClient; 
} 
它每2分鐘使用者目前活動

所以將發送到服務四。在我的服務四(意圖服務):

@Override 
protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method stub 

     if (ActivityRecognitionResult.hasResult(intent)) { 
      ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent); 
      // 
      if (result != null) { 
       // 
       Log.d("Aha", "he he"); 
      } else { 
       Log.d("Ohno", "T_T"); 
      } 

} 

它工作正常,但當我卸載應用程序。並再次安裝servicefour中的所有ActivityRecognitionResult爲空它只是可以再次正常工作,如果我重新啓動設備。我不知道如何解決這個問題。請幫助我,並感謝您的閱讀。

+0

看起來像是警告還是錯誤?那麼logcat呢? – shkschneider

+0

just this warning:01-09 17:02:09.510:W/ResourceType(18189):getEntry失敗,因爲entryIndex 27超出了類型entryCount 3 01-09 17:02:09.510:W/ResourceType(18189)在包0(錯誤-2147483647)中輸入0x7f05001b(t = 4 e = 27) 01-09 17:02:09.510:E/GooglePlayServicesUtil(18189):未找到Google Play服務資源。檢查您的項目配置以確保包含資源。 01-09 17:02:28.289:W/ResourceType(18189):getEntry失敗,因爲entryIndex 27超出了類型entryCount 3 – YenMinh

回答

0

我會發布我的代碼來識別用戶的當前活動,但用最新的api。

public class GPSLocationService extends Service implements ConnectionCallbacks, OnConnectionFailedListener { 

private String TAG = "[ServiceDetect]"; 
// 
private PendingIntent mActivityRecognitionPendingIntent; 
// Stores the current instantiation of the activity recognition client 
private GoogleApiClient mApiClient; 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // TODO Auto-generated method stub 
    if (checkGooglePlayAvaible()) { 
     startTrack(); 
    } 
    return START_STICKY; 
} 

@Override 
public void onConnected(Bundle bundle) { 
    // TODO Auto-generated method stub 
    // ------------------------------------- 
    // to remove ActivityRecognition call 
    // ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates 
    //---------------------------- 
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mApiClient, 2 * 60 * 1000, 
      createPendingRequest()); 
    // 
    mApiClient.disconnect(); 
    stopSelf(); 


} 

@Override 
public void onConnectionFailed(ConnectionResult arg0) { 
    // TODO Auto-generated method stub 
    Log.d(TAG, "onConnectionFailed"); 
} 

@Override 
public void onConnectionSuspended(int arg0) { 
    // TODO Auto-generated method stub 

    Log.d(TAG, "onConnectionSuspended"); 
    mApiClient.connect(); 
} 

public void startTrack() { 

    if (mApiClient == null) { 
     mApiClient = new GoogleApiClient.Builder(getApplicationContext()).addApi(ActivityRecognition.API) 
       .addConnectionCallbacks(this).addOnConnectionFailedListener(this).build(); 
    } 
    if (!mApiClient.isConnected() || !mApiClient.isConnecting()) { 

     mApiClient.connect(); 

    } 

} 

public PendingIntent createPendingRequest() { 
    if (null != mActivityRecognitionPendingIntent) { 

    } else { 
     Intent intent = new Intent(getApplicationContext(), ServiceFour.class); 
     mActivityRecognitionPendingIntent = PendingIntent.getService(getApplicationContext(), 2, intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
    } 
    return mActivityRecognitionPendingIntent; 
} 

public boolean checkGooglePlayAvaible() { 

    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) { 
     return true; 
    } 
    return false; 
} 

@Override 
public void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 

} 
} 

而現在在我的servicefour。我收到ActivityRecognitionResult:

@Override 
protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method stub 

     Bundle bundle = intent.getExtras(); 
     if (ActivityRecognitionResult.hasResult(intent)) { 
      if (bundle.containsKey("com.google.android.location.internal.EXTRA_ACTIVITY_RESULT")) { 
       String activityRecognitionResult = bundle 
         .getParcelable("com.google.android.location.internal.EXTRA_ACTIVITY_RESULT") + ""; 
       Log.d(TAG, "activityRecognitionResult " + activityRecognitionResult +" (^.,,.^)");         
     } 

} 
0

這裏是你的問題:

E/GooglePlayServicesUtil(18189): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included. 

你GooglePlayServices不工作。

請確保您正確包含它們(發佈您的Gradle文件等)。

+0

謝謝。我檢查過它並更新了Google Play服務。但它沒有奏效。但是我發現了一個愚蠢的方式來使它工作 – YenMinh