当应用程序为后台时,我无法在间隔时间内获得服务位置。服务有效并获取位置,但是位置未更新,获取旧位置。我通过小米进行测试,也许miui有问题?我尝试了大约5-10的方法,但结果是相同的。打开应用位置时获取旧位置信息获取新信息并关闭该应用,使用旧经纬度获取位置信息
例如第一种方法:
locationManager = getApplicationContext().getSystemService(LOCATION_SERVICE) as LocationManager
isGPSEnable = locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)
isNetworkEnable = locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
if (!isGPSEnable && !isNetworkEnable) {
} else {
Log.e(TAG,"${isGPSEnable} ")
if (isGPSEnable) {
location = null
locationManager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER, LastKnownService.LOCATION_INTERVAL.toLong(), 0f, this)
if (locationManager != null) {
location = locationManager!!.getLastKnownLocation(LocationManager.GPS_PROVIDER)
if (App.lastKnownLocation == null){
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == none")
App.lastKnownLocation = location
sendLocation(App.lastKnownLocation!!)
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
}
}else{
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == ${location!!.distanceTo(App.lastKnownLocation)}")
App.lastKnownLocation = location
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
sendLocation(App.lastKnownLocation!!)
}
}
}
}else if (isNetworkEnable) {
location = null
locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LastKnownService.LOCATION_INTERVAL.toLong(), 0f, this)
if (locationManager != null) {
location = locationManager!!.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
if (App.lastKnownLocation == null){
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == none")
App.lastKnownLocation = location
sendLocation(App.lastKnownLocation!!)
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
}
}else{
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == ${location!!.distanceTo(App.lastKnownLocation)}")
App.lastKnownLocation = location
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
sendLocation(App.lastKnownLocation!!)
}
}
}
}
}
第二种方式:
mLocationManager!!.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL.toLong(), LOCATION_DISTANCE,
mLocationListeners[1])
private var mLocationListeners = arrayOf(LocationListener(LocationManager.GPS_PROVIDER), LocationListener(LocationManager.NETWORK_PROVIDER))
private inner class LocationListener(provider: String) : android.location.LocationListener {
internal var mLastLocation: Location
init {
Log.e(TAG, "LocationListener $provider")
mLastLocation = Location(provider)
}
override fun onLocationChanged(location: Location) {
Log.e(TAG, "onLocationChanged: $location")
try{
val mBuilder = NotificationCompat.Builder(this@LocationService, "123")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("location sended")
.setContentText("onlocation changed")
.setStyle(NotificationCompat.BigTextStyle()
.bigText("onlocation changed"))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
if (notificationManager != null) notificationManager!!.notify(1,mBuilder.build())
val distance = location.distanceTo(mLastLocation)
if (distance > 10){
mLastLocation = location
sendLocation(mLastLocation)
}
}catch (e :java.lang.Exception){
Log.e(TAG, "send http lat lon exception: $e")
}
mLastLocation.set(location)
}
override fun onProviderDisabled(provider: String) {
Log.e(TAG, "onProviderDisabled: $provider")
}
override fun onProviderEnabled(provider: String) {
Log.e(TAG, "onProviderEnabled: $provider")
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {
Log.e(TAG, "onStatusChanged: $provider")
}
}
答案 0 :(得分:1)
您必须手动在“电池优化”中删除应用程序的电池优化限制(从“安全性”应用程序转到“电池”选项,为您的应用程序选择“无限制”),然后它才能工作。但是如何以编程方式进行操作,我也是寻找那个..谢谢
答案 1 :(得分:0)
调用此方法:
private fun openMiuiPowerKeeper(context: Context) {
context.startActivity(Intent().apply {
setClassName(
"com.miui.powerkeeper",
"com.miui.powerkeeper.ui.HiddenAppsConfigActivity"
)
putExtra("package_name", context.packageName)
putExtra("package_label", context.getString(R.string.app_name))
flags = Intent.FLAG_ACTIVITY_NEW_TASK
})
}
它打开的窗口,用户需要在该行“非限制”。您需要检查设备上的miui固件或其他类似的东西,例如:
private fun isFirmwareMiui(context: Context): Boolean {
if (Build.MANUFACTURER.equals("xiaomi", true)) {
context.packageManager.getInstalledPackages(PackageManager.GET_META_DATA).forEach {
return it.packageName.startsWith("com.miui.")
}
}
return false
}