ジオフェンスGEOFENCE_TRANSITION_ENTER
またはGEOFENCE_TRANSITION_DWELL
ジオフェンスをターゲット座標(場所F)に登録する必要があります。あなたの活動/フラグメントのonCreateで
あなたはAPIクライアントを作成する必要があります。次にonConnected
にあなたが次のことを行う必要があります
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
も接続/切断することを忘れない
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient,
geofenceRequest,
pendingIntent)
ジオフェンスを一度追加する必要があります。
ここで、geofenceRequestはGeofencingRequestを使用して作成されます。ビルダー:
public class GeofenceTransitionsIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (!geofencingEvent.hasError()) {
int geofenceTransition = geofencingEvent.getGeofenceTransition();
if (geofenceTransition != -1) {
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
if (triggeringGeofences != null && triggeringGeofences.size() > 0) {
Geofence geofence = triggeringGeofences.get(0);
// Do something with the geofence, e.g. show a notification using NotificationCompat.Builder
}
}
}
}
}
あなたのマニフェストにこのサービスを登録することを忘れないでください:
<service android:name=".GeofenceTransitionsIntentService"/>
geofenceRequest = new GeofencingRequest.Builder().addGeofence(yourGeofence).build()
yourGeofenceとpendingIntent:GeofenceTransitionsIntentServiceは次のように可能性があり
yourGeofence = new Geofence.Builder()....build(); // Here you have to set the coordinate of Place F and GEOFENCE_TRANSITION_ENTER/GEOFENCE_TRANSITION_DWELL
pendingIntent = PendingIntent.getService(this,
(int)(System.currentTimeMillis()/1000),
new Intent(this, GeofenceTransitionsIntentService.class),
PendingIntent.FLAG_UPDATE_CURRENT);
アプリが閉じていてもGeofenceTransitionsIntentService.onHandleIntent()
が呼び出されます。
希望します。
公式文書 –