地理围栏 api 无法在 Android 中以未决意图工作
我正在尝试实现简单的Geofence应用程序,但根本没有被触发。
这是我到目前为止所做的工作的代码 -
class GeoFencingHelper(context: Context, private val task: Task, private val pendingIntent: PendingIntent){
private val geofencingClient = LocationServices.getGeofencingClient(context)
init {
Timber.e(task.id.toString())
}
@SuppressLint("MissingPermission")
fun initiateGeoFencing() {
val geoReq = createGeoFenceList()
geofencingClient.addGeofences(geoReq, pendingIntent).apply {
addOnSuccessListener {
Timber.e("added geofence!")
}
addOnFailureListener {
Timber.e("geofence failed!")
}
}
}
private fun createGeoFenceList(): GeofencingRequest {
val geofenceList = arrayListOf<Geofence>()
geofenceList.add(Geofence.Builder().apply {
setRequestId(task.id.toString())
setCircularRegion(
task.location.latitude,
task.location.longitude,
task.range.toFloat()
)
// Set the expiration duration of the geofence. This geofence gets automatically
// removed after this period of time.
setExpirationDuration(Geofence.NEVER_EXPIRE)
setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
// Create the geofence.
}.build())
return getGeofencingRequest(geofenceList)
}
private fun getGeofencingRequest(
fenceList: List<Geofence>,
): GeofencingRequest {
return GeofencingRequest.Builder().apply {
setInitialTrigger(
if(task.reminderCondition == ReminderCondition.ON_ENTRY)
GeofencingRequest.INITIAL_TRIGGER_ENTER
else
GeofencingRequest.INITIAL_TRIGGER_EXIT)
addGeofences(fenceList)
}.build()}
我在这里添加地理围栏是广播接收器
fun createGeoFence(context: Context, task: Task){
val geofencePendingIntent: PendingIntent by lazy {
task.range = 150.0
val intent = Intent(context, ReminderNotificationBroadcastReceiver::class.java)
intent.putExtra(TASK_ID, task.id)
if(Build.VERSION.SDK_INT > 30){
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
}else{
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}
val geoFencingHelper = GeoFencingHelper(context, task, geofencePendingIntent)
geoFencingHelper.initiateGeoFencing()}
,该广播接收器是在触发地理五项事件时向用户显示通知的广播接收器。
class ReminderNotificationBroadcastReceiver @Inject constructor(private val dao: TaskDao): BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val builder = NotificationCompat.Builder(context, "2222")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Reminder \"\".")
.setContentText("Reminder \".")
.setOngoing(true)
.setColor(Color.BLUE)
with(NotificationManagerCompat.from(context)) {
notify(2222, builder.build())
}}}
到目前为止,我已经做过的事情 -
- 残疾电池优化。
- 始终允许背景位置权限。
问题 -
我使用了Lockito和GPS仿真器(例如Lockito和GPS模拟器)的模拟位置应用程序来测试该应用。 但是,Geofence事件没有被触发,我还尝试构建和测试由Android Codelab提供的样本项目,但我也在该应用程序中面临同样的问题。
I'm trying to implement simple geofence app but its not getting triggered at all.
here is the code of what I've done so far -
class GeoFencingHelper(context: Context, private val task: Task, private val pendingIntent: PendingIntent){
private val geofencingClient = LocationServices.getGeofencingClient(context)
init {
Timber.e(task.id.toString())
}
@SuppressLint("MissingPermission")
fun initiateGeoFencing() {
val geoReq = createGeoFenceList()
geofencingClient.addGeofences(geoReq, pendingIntent).apply {
addOnSuccessListener {
Timber.e("added geofence!")
}
addOnFailureListener {
Timber.e("geofence failed!")
}
}
}
private fun createGeoFenceList(): GeofencingRequest {
val geofenceList = arrayListOf<Geofence>()
geofenceList.add(Geofence.Builder().apply {
setRequestId(task.id.toString())
setCircularRegion(
task.location.latitude,
task.location.longitude,
task.range.toFloat()
)
// Set the expiration duration of the geofence. This geofence gets automatically
// removed after this period of time.
setExpirationDuration(Geofence.NEVER_EXPIRE)
setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
// Create the geofence.
}.build())
return getGeofencingRequest(geofenceList)
}
private fun getGeofencingRequest(
fenceList: List<Geofence>,
): GeofencingRequest {
return GeofencingRequest.Builder().apply {
setInitialTrigger(
if(task.reminderCondition == ReminderCondition.ON_ENTRY)
GeofencingRequest.INITIAL_TRIGGER_ENTER
else
GeofencingRequest.INITIAL_TRIGGER_EXIT)
addGeofences(fenceList)
}.build()}
I'm adding geofencing here
fun createGeoFence(context: Context, task: Task){
val geofencePendingIntent: PendingIntent by lazy {
task.range = 150.0
val intent = Intent(context, ReminderNotificationBroadcastReceiver::class.java)
intent.putExtra(TASK_ID, task.id)
if(Build.VERSION.SDK_INT > 30){
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
}else{
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}
val geoFencingHelper = GeoFencingHelper(context, task, geofencePendingIntent)
geoFencingHelper.initiateGeoFencing()}
here is the broadcast receiver to show notification to user when geofence event gets triggered.
class ReminderNotificationBroadcastReceiver @Inject constructor(private val dao: TaskDao): BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val builder = NotificationCompat.Builder(context, "2222")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Reminder \"\".")
.setContentText("Reminder \".")
.setOngoing(true)
.setColor(Color.BLUE)
with(NotificationManagerCompat.from(context)) {
notify(2222, builder.build())
}}}
things I've done so far -
- disabled battery optimization.
- background location permission is allowed all the time.
Issue -
I used mock location apps like lockito and gps emulator to test the app.
but the geofence event is not getting triggered, I also tried to build and test sample project provided by android codelab, but I'm facing same issue in that app as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于SO和网上也有类似的问题,但简而言之,Google Codelab并非最新。
pENDENTINTENT
用于地理申请的才能使其工作。因此,在您的情况下:或这样的情况下,如果 作为pendentIntent的,默认情况下是可以突变的,直到
build> build> build.version_codes.r
:There are similar questions on SO and on the net but in short, Google codelab is not up to date for this. The
PendingIntent
used for Geofencing needs to be mutable for it to work. So in your case:or just like this without the
if
as PendingIntent is mutable by default up untilBuild.VERSION_CODES.R
: