2016-08-11 5 views
9

모든 것이 올바르게 빌드되어 에뮬레이터에서 실행되지만 내 IntentService에 아무것도 기록하지 못하는 것 같습니다. 내가 누락되거나 간과 할 수있는 기본적인 것들이있을 것이라고 확신하지만, 나는 안드로이드/자바에 매우 익숙하며,이 시점에서 아이디어가 부족하다. 내가 에뮬레이터 또는 안드로이드 콘솔을 사용하여/LNG 위도 해당 장치를 업데이트 할 때마다 Android Geofence 이벤트에서 의도를 수신하지 못했습니다.

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

private Geofence geofence; 
private PendingIntent mGeofencePendingIntent; 
private GoogleApiClient mGoogleApiClient; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    LatLng latLng = new LatLng(39.7492350, -104.9913250); 

    geofence = new Geofence.Builder() 
      .setRequestId(GEOFENCE_REQ_ID) 
      .setCircularRegion(latLng.latitude, latLng.longitude, GEOFENCE_RADIUS_IN_METERS) 
      .setExpirationDuration(GEOFENCE_EXPIRATION) 
      .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) 
      .build(); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 
    mGoogleApiClient.connect(); 
} 

private boolean checkPermission() { 
    Log.i(TAG, "MainActivity.checkPermission()"); 
    return (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED); 
} 

private GeofencingRequest getGeofencingRequest() { 
    Log.i(TAG, "MainActivity.getGeofencingRequest()"); 
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); 
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); 
    builder.addGeofence(geofence); 
    return builder.build(); 
} 

private PendingIntent getGeofencePendingIntent() { 
    Log.i(TAG, "MainActivity.getGeofencePendingIntent()"); 
    if (null != mGeofencePendingIntent) { 
     return mGeofencePendingIntent; 
    } 

    Intent intent = new Intent(this, GeofenceTransitionsIntentService.class); 
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

@Override 
public void onConnected(Bundle connectionHint) { 
    Log.i(TAG, "MainActivity.onConnected()"); 
    if (checkPermission()) { 
     mGeofencePendingIntent = getGeofencePendingIntent(); 
     LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, getGeofencingRequest(), mGeofencePendingIntent); 
    } else { 
     Log.i(TAG, "Permission not granted"); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.i(TAG, "MainActivity.onConnectionSuspended()"); 
    if (null != mGeofencePendingIntent) { 
     LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, mGeofencePendingIntent); 
    } 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    Log.i(TAG, "MainActivity.onConnectionFailed()"); 
}} 

, 아니 의도는 다음과 같은 서비스에 의해 수신되지 :

public class GeofenceTransitionsIntentService extends IntentService { 

public GeofenceTransitionsIntentService() { 
    super(GeofenceTransitionsIntentService.class.getSimpleName()); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    Log.i(TAG, "GeofenceTransitionsIntentService.onHandleIntent()"); 

    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); 
    if (geofencingEvent.hasError()) { 
     int errorCode = geofencingEvent.getErrorCode(); 
     Log.e(TAG, "Location Services error: " + errorCode); 
    } else { 
     Log.i(TAG, "geofencingEvent was successful"); 
    } 
}} 

매니페스트 :

<?xml version="1.0" encoding="utf-8"?> 

<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"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service android:name=".GeofenceTransitionsIntentService" /> 
</application> 

다음 리소스를 참조했습니다 : Android DocumentationGoogle Sample.

+1

몇 가지 명확하지 않은 사항 : 1 - 실제 장치에서 테스트 해 보셨습니까? 2 - 지정한 위도/경도에 있습니까? 3 가지 Google Play 서비스가 설치되어 있나요? 4 - Google지도를 열고 Lat/Lng를 업데이트 해 보셨습니까? 경우에 따라 GPS를 에뮬레이터에서 작동시키는 유일한 방법입니다. – fernandospr

+0

실제 장치에서 앱을 실행 한 다음 알려주십시오. –

답변

0

에뮬레이터에서 위치 서비스가 제대로 작동하지 않는 것 같습니다. 일단 실제 장치를 연결하면 모든 것이 예상대로 작동했습니다.