Android 土工栅栏不触发
我正在尝试在我的应用程序上实现Android 土工栅栏不触发,android,google-maps,geofencing,Android,Google Maps,Geofencing,我正在尝试在我的应用程序上实现地理围栏,问题是当我在标记的位置时,它不会触发 我要做的是从一个角度设置纬度&经度 我得到的值很好,因为我把它放在上面,位置是正确的,因为我读过很多答案,人们从lat/long设置了不好的值 我有两个类:GeofenceSingleton.class和GeofenceTransitionsIntentService GeofenceSingleton.class如下所示: public class GeofenceSingleton implements Googl
地理围栏
,问题是当我在标记的位置时,它不会触发
我要做的是从一个角度设置纬度
&经度
我得到的值很好,因为我把它放在上面,位置是正确的,因为我读过很多答案,人们从lat/long设置了不好的值
我有两个类:GeofenceSingleton.class
和GeofenceTransitionsIntentService
GeofenceSingleton.class
如下所示:
public class GeofenceSingleton implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {
private GoogleApiClient googleApiClient;
private static GeofenceSingleton _singleInstance;
private static Context appContext;
private static final String ERR_MSG = "Application Context is not set!! " +
"Please call GeofenceSngleton.init() with proper application context";
private PendingIntent mGeofencePendingIntent;
private static ArrayList<Geofence> mGeofenceList;
private GeofenceSingleton() {
if (appContext == null)
throw new IllegalStateException(ERR_MSG);
this.googleApiClient = new GoogleApiClient.Builder(this.appContext)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
this.googleApiClient.connect();
mGeofenceList = new ArrayList<Geofence>();
}
/**
* @param applicationContext
*/
public static void init(Context applicationContext) {
appContext = applicationContext;
}
public static GeofenceSingleton getInstance() {
if (_singleInstance == null)
synchronized (GeofenceSingleton.class) {
if (_singleInstance == null)
_singleInstance = new GeofenceSingleton();
}
return _singleInstance;
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public void addGeofence(Location location, String uid) {
mGeofenceList.add(new Geofence.Builder()
.setRequestId(uid)
.setCircularRegion(
location.getLatitude(),
location.getLongitude(),
500
)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.setExpirationDuration(1000000)
.build());
}
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(mGeofenceList);
return builder.build();
}
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(appContext, GeofenceSingleton.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
// addGeofences() and removeGeofences().
return PendingIntent.getService(appContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public void startGeofencing() {
if (!googleApiClient.isConnected()) {
Toast.makeText(appContext, "API client not connected", Toast.LENGTH_SHORT).show();
return;
}
LocationServices.GeofencingApi.addGeofences(
googleApiClient,
// The GeofenceRequest object.
getGeofencingRequest(),
// A pending intent that that is reused when calling removeGeofences(). This
// pending intent is used to generate an intent when a matched geofence
// transition is observed.
getGeofencePendingIntent()
).setResultCallback(this); // Result processed in onResult().
Toast.makeText(appContext,"Geofencing started", Toast.LENGTH_LONG).show();
}
public void removeGeofence(){
LocationServices.GeofencingApi.removeGeofences(
googleApiClient,
// This is the same pending intent that was used in addGeofences().
getGeofencePendingIntent()
);
}
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
// Update state and save in shared preferences.
Toast.makeText(
appContext,
"YO",
Toast.LENGTH_SHORT
).show();
} else {
Toast.makeText(
appContext,
"NOO",
Toast.LENGTH_SHORT
).show();
}
}
}
在我的main活动中
GeofenceSingleton.init(this); //If I don't call this the class doesn't work
this.geofenceSingleton = GeofenceSingleton.getInstance();
然后我构建了一个地理围栏
,如下所示:
Location geofence = new Location("");
geofence.setLatitude(Double.parseDouble(latitude));
geofence.setLongitude(Double.parseDouble(longitude));
geofenceSingleton.addGeofence(geofence, GeofenceKey);
geofenceSingleton.startGeofencing();
笔记
我已经在我的manifest.xml
我将服务
声明为
我在我的build.gradle
:compile'com.google.android.gms:play services:7.8.0'
它只显示Toast.makeText(appContext,“YO”,Toast.LENGTH_SHORT).show()代码>这是因为状态是success,并且还显示了Toast.makeText(appContext,“geobinging start”,Toast.LENGTH_LONG).show()代码>表示它已启动
问题:
为什么从未调用myIntentService
我在我家放了一个Lat
/Long
,对它进行了测试,既然我在上面,它应该会被触发,不是吗?正如我已经回答的,Geofence API
有点像被动定位接收器,也就是说,当您需要地理围栏
工作时,您必须同时从LocationServices.FusedLocationApi.RequestLocationUpdate
请求坐标。它永远不会工作,因为您所做的是
Intent intent = new Intent(appContext, GeofenceSingleton.class);
将意向传递给单身汉
你需要做的是
Intent intent = new Intent(appContext, GeofenceTransitionsIntentService.class);
如果您的意图已使用适当的类构造(请参阅),则还需要确保服务也包含在AndroidManifest.xml
中
<application
...
<service android:name=".GeofencingTransitionIntentService" />
确保您的服务和广播接收在清单中启用了这些属性
android:enabled="true"
android:exported="true"
工作正常吗?顺便说一句,我用这3个文件创建了一个新项目,然后试着去做,结果成功了,所以我也会为你工作。@Skizo ozᴉʞS我可以获得进入通知,但无法获得退出通知FIRED@Skizo-奥兹ᴉʞS同样,当我给出的半径小于25米时,我没有收到进入通知。知道它为什么不工作吗???退出通知不会被触发,因为您已经设置了TransitionTypes(Geofence.Geofence_TRANSITION_ENTER)您还需要添加GEOFENCE\u TRANSITION\u ENTER以获得退出转换。您不需要分配权限吗?您要导出它吗?
android:enabled="true"
android:exported="true"