2015-01-31 2 views
0

내 앱의 활동 승인 업데이트를 받고 싶지만 부분적으로 만 작동하는 것으로 보입니다. 우선은 디버그 로그 명중 된 ActivityRecognitionIntentService onHandleIntent을 의미한다 활동 유형() 표시하지만 그것은 결코 ActivityBroadCastReceiver로하지 않습니다 동안이에서 작업하지만 가끔되지 않은 생각ActivityRecognitionIntentService onHandleIntent 가끔만 발생합니다.

나는 MainActivity

에 다음 코드를
Intent i = new Intent(this, ActivityRecognitionIntentService.class); 
    i.setAction("ActivityRecognitionIntentService"); 
    PendingIntent activityRecognitionPendingIntent = PendingIntent.getService(this, 7422, i, PendingIntent.FLAG_UPDATE_CURRENT); 
    ActivityRecognitionApi rec = ActivityRecognition.ActivityRecognitionApi; 
    rec.requestActivityUpdates(mClient, 5000, activityRecognitionPendingIntent) 
      .setResultCallback(new ResultCallback<Status>() { 
       @Override 
       public void onResult(Status status) { 
        if (status.isSuccess()) { 
         Log.i(TAG, "Successfully registered updates"); 
        } else { 
         Log.i(TAG, "Failed to register updates"); 
        } 
       } 
      });; 

내가 제대로 한 경우 매 5 초마다 작동해야합니다.

public class ActivityRecognitionIntentService extends IntentService { 

ActivityRecognitionResult result; 
Intent i; 
DetectedActivity mpactivity; 

public ActivityRecognitionIntentService() { 
    super("ActivityRecognitionIntentService"); 
    i = new Intent("ACTIVITY_RECOGNITION_DATA"); 
    Log.d(MainActivity.TAG, "ActivityRecognitionIntentService created"); 
} 

private String getTypes(int type) { 
    Log.d(MainActivity.TAG, "type = " + type); 
    if(type == DetectedActivity.UNKNOWN) 
     return "Unknown"; 
    else if(type == DetectedActivity.IN_VEHICLE) 
     return "In Vehicle"; 
    else if(type == DetectedActivity.ON_BICYCLE) 
     return "On Bicycle"; 
    else if(type == DetectedActivity.RUNNING) 
     return "Running"; 
    else if(type == DetectedActivity.ON_FOOT) 
     return "On Foot"; 
    else if(type == DetectedActivity.STILL) 
     return "Still"; 
    else if(type == DetectedActivity.TILTING) 
     return "Tilting"; 
    else if(type == DetectedActivity.WALKING) 
     return "Walking"; 
    else 
     return ""; 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    Log.d(MainActivity.TAG, "onHandleIntent"); 
    if (intent.getAction() == "ActivityRecognitionIntentService") { 
     if(ActivityRecognitionResult.hasResult(intent)){ 
      result = ActivityRecognitionResult.extractResult(intent); 
      mpactivity = result.getMostProbableActivity(); 
      i.putExtra("Activity", getTypes(mpactivity.getType())); 
      i.putExtra("Confidence", mpactivity.getConfidence()); 
      LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i); 
     } 
    } 
} 

내 매니페스트 쇼 : 연관된 IntentService이있는 브로드 캐스트 리시버와

<service android:name=".ActivityRecognitionIntentService" android:exported="false"></service> 

    <receiver android:name=".ActivityBroadcastReceiver" android:exported="false"> 
     <intent-filter> 
      <action android:name="ACTIVITY_RECOGNITION_DATA"/> 
     </intent-filter> 
    </receiver> 
    <meta-data android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

:

public class ActivityBroadcastReceiver extends android.content.BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    Log.d(MainActivity.TAG, "BroadcastReceiver"); 
    Toast.makeText(context, "Service received", Toast.LENGTH_SHORT).show(); 
} 

왜 가끔 일 것이다? 전화가 디버깅하는 동안 잠자기가 허용되지 않습니다 (N6). 또한, 왜 그것이 BroadcastReceiver를 만들지 못합니까?

답변

2

detectionIntervalMillis은 정확하지 않습니다. 전화가 여전히있는 경우 활동이 변경 될 때까지 아무 것도보고하지 않을 수 있습니다. 내 경험상, 현재 활동이 변경되지 않으면 간격이 길어집니다.

문서에서 인용하려면 To conserve battery, activity reporting may stop when the device is 'STILL' for an extended period of time. It will resume once the device moves again. 기기가 아직 켜져 있습니까? 약간의 주위를 흔들거나 걸어서 시험 해보십시오.

+0

이제 나는 바보처럼 느껴집니다. 나는 그것을 보았다. 나는 그것을 흔들어 보려고했으나 효과가 없었다. 이제는 내 주변 방송국의 문제를 해결해야만했습니다. 또한, 바보 이동 번호 2, 내가 코멘트를 저장하기 전에이 페이지를 새로 고쳤습니다 : ( – easycheese

+1

LocalBroadcastManager.getInstance (getApplicationContext())를 변경했다 그것을 알아 냈어, sendBroadcast (i); this.sendBroadcast (i); – easycheese

관련 문제