我與來自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爲空它只是可以再次正常工作,如果我重新啓動設備。我不知道如何解決這個問題。請幫助我,並感謝您的閱讀。
看起來像是警告還是錯誤?那麼logcat呢? – shkschneider
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