我已经运行了Geofence样本,并且已经分配了两组Lat,Lng和radius并运行样本。当我点击寄存器地理围栏时,我正在记录日志中的内容
Location Services is available
Client connected
Add Geofences: Success GeofenceRequestIds=[1, 2]
com.example.android.geofence.ACTION_GEOFENCES_ADDED
但之后没有任何反应,我不知道我通过启用gps但没有效果通过当前位置的问题。
我不知道什么时候我会在越过栅栏时收到通知,为了测试它我已经通过了击剑中相同的地理坐标,但仍然没有警报。
在stackoverflow上读取几个线程后,我甚至尝试在getService()
函数内用getBroadcast()
替换createRequestPendingIntent
,但没有运气。
我也尝试了
**Geofence #1 and Geofence #2**
Lat : 23.039568000000003
Lng : 72.56600400000002
Radius :1
我还将我目前在genymotion中的位置设置为高于坐标,以使其工作但没有通知。
即使在真实的设备上,我的3G也已启用,我正在使用该设备,但到目前为止没有任何变化。
答案 0 :(得分:2)
你设置半径太小google-play服务当你增加半径时不给1米通知它给出更好的结果并尝试进行地理围栏。
GeofenceReceiver中的
Intent broadcastIntent = new Intent();
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);
if (LocationClient.hasError(intent)) {
handleError(intent);
} else {
handleEnterExit(intent);
}
}
private void handleError(Intent intent) {
// Get the error code
int errorCode = LocationClient.getErrorCode(intent);
// Get the error message
String errorMessage = LocationServiceErrorMessages.getErrorString(
context, errorCode);
// Log the error
Log.e(GeofenceUtils.APPTAG, context.getString(
R.string.geofence_transition_error_detail, errorMessage));
// Set the action and error message for the broadcast intent
broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)
.putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, errorMessage);
// Broadcast the error *locally* to other components in this app
LocalBroadcastManager.getInstance(context).sendBroadcast(
broadcastIntent);
}
private void handleEnterExit(Intent intent) {
// Get the type of transition (entry or exit)
int transition = LocationClient.getGeofenceTransition(intent);
// Test that a valid transition was reported
if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
|| (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {
// Post a notification
List<Geofence> geofences = LocationClient
.getTriggeringGeofences(intent);
String[] geofenceIds = new String[geofences.size()];
String ids = TextUtils.join(GeofenceUtils.GEOFENCE_ID_DELIMITER,
geofenceIds);
String transitionType = getTransitionString(transition);
sendNotification(transitionType, ids);
for (int index = 0; index < geofences.size(); index++) {
Geofence geofence = geofences.get(index);
}
// Create an Intent to broadcast to the app
broadcastIntent
.setAction(GeofenceUtils.ACTION_GEOFENCE_TRANSITION)
.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);
LocalBroadcastManager.getInstance(sqlitewraper.context)
.sendBroadcast(broadcastIntent);
// Log the transition type and a message
Log.d(GeofenceUtils.APPTAG, transitionType + ": " + ids);
Log.d(GeofenceUtils.APPTAG, context
.getString(R.string.geofence_transition_notification_text));
// In debug mode, log the result
Log.d(GeofenceUtils.APPTAG, "transition");
// An invalid transition was reported
} else {
// Always log as an error
Log.e(GeofenceUtils.APPTAG, context.getString(
R.string.geofence_transition_invalid_type, transition));
}
}
/**
* Posts a notification in the notification bar when a transition is
* detected. If the user clicks the notification, control goes to the main
* Activity.
*
* @param transitionType
* The type of transition that occurred.
*
*/
private void sendNotification(String transitionType, String locationName) {
// Create an explicit content Intent that starts the main Activity
Intent notificationIntent = new Intent(sqlitewraper.context,
MainActivity.class);
// Construct a task stack
TaskStackBuilder stackBuilder = TaskStackBuilder
.create(sqlitewraper.context);
// Adds the main Activity to the task stack as the parent
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack
PendingIntent notificationPendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(
sqlitewraper.context);
// Set the notification contents
builder.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(transitionType + ": " + locationName)
.setContentText(
sqlitewraper.context
.getString(R.string.geofence_transition_notification_text))
.setContentIntent(notificationPendingIntent);
// Get an instance of the Notification manager
NotificationManager mNotificationManager = (NotificationManager) sqlitewraper.context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
}
private String getTransitionString(int transitionType) {
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
return ("enter geofence");
case Geofence.GEOFENCE_TRANSITION_EXIT:
return ("exit from geofence");
default:
return ("Transisation unknown");
}
}
更改示例代码GeofenceRequester
Intent intent = new Intent("packagename.ACTION_RECEIVE_GEOFENCE");
// Intent intent = new Intent(mActivity, ReceiveTransitionsIntentService.class);
return PendingIntent.getBroadcast(
sqlitewraper.context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
更改清单文件
<receiver android:name="packagename.GeofenceReceiver"
android:exported="false" >
<intent-filter>
<action android:name="packagename.ACTION_RECEIVE_GEOFENCE" />
<category android:name="packagename" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
嘿,一旦检查是否选择了访问我的位置。即设置 - &gt;位置访问