我正在创建一个应用程序,该应用程序将简单地注册一些地理围栏,并在用户输入这些地理围栏后通知用户。
为此,我遵循了一些资源 *并创建了一个适当的 code **
代码运行正常,即使在后台,应用程序也会收到地理围栏通知。
但是我想针对“自适应电池应用程序限制” ***(也称为android 9+中引入的“ App Standby Buckets”)进一步测试我的应用程序,该应用程序将应用程序分为4个主要存储桶取决于用户的使用情况。
为此,我使用了以下命令:
adb shell dumpsys battery unplug
adb shell am set-standby-bucket com.polygonfence frequent
这导致应用程序移至频繁使用的buket。
问题是无论我将应用程序移至稀有或频繁的buket时,我都停止接收更新。无论我进出栅栏多少次(要进进出出,我都在使用 fake gps应用 ****)
要开始接收更新,我要么将我的应用移至(活动或正在工作的状态),要么重新接通电源
adb shell am set-standby-bucket com.polygonfence active
adb shell dumpsys battery reset
这是预期的行为吗?还是我的代码缺少某些内容?还是我以错误的方式进行测试?
最后一点要注意的是,在我的个人设备上,我很惊讶甚至youtube也被认为是常用应用程序,因此我认为大多数情况下我的应用程序都不会处于活动状态或工作状态用户。
资源 *
https://developer.android.com/training/location/geofencing
https://www.raywenderlich.com/7372-geofencing-api-tutorial-for-android
代码 **
class GeofenceHandler(val context: Context) {
private val geofencingClient = LocationServices.getGeofencingClient(context)
private val geofencePendingIntent: PendingIntent by lazy {
val intent = Intent(context, GeofenceBroadcastReceiver::class.java)
PendingIntent.getBroadcast(
context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT)
}
@SuppressLint("MissingPermission")
fun add(fenceEntry: FenceEntry,
success: (reminder: FenceEntry) -> Unit,
failure: (error: String) -> Unit) {
val geofence = buildGeofence(fenceEntry)
geofencingClient
.addGeofences(buildGeofencingRequest(geofence), geofencePendingIntent)
.addOnSuccessListener {
Repo().saveFence(context ,fenceEntry )
success(fenceEntry)
}
.addOnFailureListener {
failure(GeofenceErrorMessages.getErrorString(context, it))
}
}
private fun buildGeofence(fenceEntry: FenceEntry): Geofence {
val latitude = fenceEntry.latLng.latitude
val longitude = fenceEntry.latLng.longitude
val radius = fenceEntry.radius
return Geofence.Builder()
.setNotificationResponsiveness(1 * 60 *1000)
.setRequestId(fenceEntry.id)
.setCircularRegion(
latitude,
longitude,
radius.toFloat()
)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build()
}
private fun buildGeofencingRequest(geofence: Geofence): GeofencingRequest {
return GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.addGeofences(listOf(geofence))
.build()
}
}
class GeofenceBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
GeofenceTransitionsJobIntentService.enqueueWork(context, intent)
}
}
“自适应电池应用限制” ***
https://developer.android.com/about/versions/pie/power#buckets
https://www.youtube.com/watch?v=-7eZL3XRqas&t=256s
假gps应用 ****
https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=en