уже несколько дней я застрял. Я пытаюсь сделать простой тост, когда пользователь входит или выходит из геозоны, чтобы увидеть, работает ли она. И НЕТ ХОРОШЕГО пошагового руководства в Интернете, которое показывает мне, как это сделать. (Те, что от Google, не показывают мне, как тосты или вообще что-либо делать...)
Вот код моей основной деятельности.
public class GeoFence extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {
private static final String GEOFENCE_ID = "geoFenceID" ;
protected GoogleApiClient mGoogleApiClient;
private Button mAddGeofencesButton;
private Button startLocationMonitoringButton;
private Button startGeoFenceMonitoringButton;
private Button stopGeoFenceMonitoringButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_geo_fence);
mAddGeofencesButton = (Button) findViewById(R.id.add_geofences_button);
startLocationMonitoringButton = (Button) findViewById(R.id.geoButton1);
startLocationMonitoringButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startLocationMonitoring();
}
});
startGeoFenceMonitoringButton = (Button) findViewById(R.id.geoButton2);
startGeoFenceMonitoringButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startGeofenceMonitoring();
}
});
stopGeoFenceMonitoringButton = (Button) findViewById(R.id.geoButton3);
stopGeoFenceMonitoringButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopGeoFenceMonitoring();
}
});
}
private void startLocationMonitoring() //Make a button for this
{
try {
LocationRequest locationRequest = LocationRequest.create().setInterval(10000)
.setFastestInterval(5000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, new LocationListener() {
@Override
public void onLocationChanged(Location location)
{
}
});
}
catch (SecurityException e)
{
}
}
private void startGeofenceMonitoring() //Make a button for this
{
try
{
Geofence geofence = new Geofence.Builder()
.setRequestId(GEOFENCE_ID)
.setCircularRegion(34.065866, -118.459572,45)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setNotificationResponsiveness(1000)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build();
GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.addGeofence(geofence).build();
Intent intent = new Intent(this, GeofenceService.class);
PendingIntent pendingIntent = PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
if(!mGoogleApiClient.isConnected())
{
//Toast not connected
}
else
{
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, geofencingRequest,pendingIntent)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if(status.isSuccess())
{
Toast.makeText(getBaseContext(),"successful monitoring...",Toast.LENGTH_SHORT).show();
}
else
{
//Something fucked up with our geofence bro
}
}
});
}
}
catch (SecurityException e)
{
}
}
private void stopGeoFenceMonitoring()
{
ArrayList<String> geofenceIds = new ArrayList<>();
geofenceIds.add(GEOFENCE_ID);
LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient,geofenceIds);
}
}
Вот мой класс GeofenceService, где, я думаю, я должен произнести тост?
public class GeofenceService extends IntentService
{
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public static final String TAG = "GeofenceService";
public GeofenceService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent)
{
GeofencingEvent event = GeofencingEvent.fromIntent(intent);
if(event.hasError())
{
}
else
{
int transition = event.getGeofenceTransition();
List<Geofence> geofences = event.getTriggeringGeofences();
Geofence geofence = geofences.get(0);
String requestId = geofence.getRequestId();
if(transition == Geofence.GEOFENCE_TRANSITION_ENTER)
{
Toast.makeText(getBaseContext(),"Entering GeoFence",Toast.LENGTH_SHORT).show();
}
else if(transition == Geofence.GEOFENCE_TRANSITION_EXIT)
{
Toast.makeText(getBaseContext(),"Leaving GeoFence",Toast.LENGTH_SHORT).show();
}
}
}
}
Наконец вот мой xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/add_geofences_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:onClick="addGeofencesButtonHandler"
android:text="Add GeoFences" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Location Monitoring"
android:id="@+id/geoButton1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start GeoFence Monitoring"
android:id="@+id/geoButton2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop GeoFence Monitoring"
android:id="@+id/geoButton3"/>
</LinearLayout>
Если у кого-то из вас, ребята, есть ПРОСТЫЕ пошаговые руководства или код о том, как сделать простую геозону с отображением всплывающего уведомления о входе или выходе из геозоны, это было бы здорово! И/или вы также можете проверить мой существующий код, чтобы увидеть, не пропустил ли я что-то. КОД ЗАПУСКАЕТСЯ, но ничего не происходит...