2016-08-11 6 views
9

すべてが正しくビルドされ、エミュレータで実行されますが、IntentServiceにログを記録できないようです。私は欠けているか見落としている基本的なものがあると確信していますが、私はAndroid/Javaにはかなり新しく、この時点でアイデアがなくなっています。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()"); 
}} 

IエミュレータまたはAndroidコンソールを使用して/ LNG LATそのデバイスを更新するたびに、何の意図は次のサービスによって受信されない:

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マップを開いて緯度/経度を更新しようとしましたか?場合によっては、GPSをエミュレータ上で動作させる唯一の方法です。 – fernandospr

+0

実際のデバイスでアプリを実行してから、教えてください。 –

答えて

0

ロケーションサービスはエミュレータで完全に機能しているようには見えません。実際のデバイスを接続すると、すべてが期待通りに機能しました。

関連する問題