好几天我现在卡住了。当用户进入或离开地理围栏以查看其是否正常工作时,我正在尝试做一个简单的吐司。并且网上没有一步一步的教程,告诉我如何做到这一点。 (谷歌的那些没有告诉我如何敬酒或做任何事情......)
这是我的主要活动代码。
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>
如果你们中的任何一个人一步一步地完成了SIMPLE(没有代码丢失)的教程或代码,关于如何使用显示器进入或退出地理围栏的简单地理围栏做得很棒!和/或你还可以检查我现有的代码,看看我是否遗漏了一些东西。代码运行,但没有任何反应......
答案 0 :(得分:0)
有同样的问题。 GoogleApiClient
现在已描述。此类被其他几个地方分裂:https://developers.google.com/android/guides/google-api-client。现在,您需要使用GeofenceClient
,它的方法非常相似。
https://developer.android.com/training/location/geofencing
简而言之,您只需要使用GeofencingClient
而不是GoogleApiClient
。
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
// calling addGeofences() and removeGeofences().
mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.
FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
使用上面提供的链接获取完整示例。