[英]android location - cannot get location in service when app is background
I cannot get location in service with interval time, when app is background. 当应用程序为后台时,我无法获得间隔时间的服务位置。 Service works and gets location but location is not updating, gets old location. 服务有效并获取位置,但是位置未更新,获取旧位置。 I test via xiaomi, maybe problem with miui? 我通过小米进行测试,也许miui有问题? I tried about 5-10 way but result is same. 我尝试了大约5-10的方法,但结果是相同的。 Get location old when I open app location get new and close the app, get location with old latitude and longitude 打开应用位置时获取旧位置信息获取新信息并关闭应用,使用旧经纬度获取位置信息
for example first way: 例如第一种方式:
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!!)
}
}
}
}
}
second way: 第二种方式:
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")
}
}
您必须手动在“电池优化”中删除应用程序的电池优化限制(从“安全性”应用程序转到“电池”选项,为您的应用程序选择“无限制”),然后它才能工作。但是如何以编程方式进行操作,我也在寻找..谢谢
call this method: 调用此方法:
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
})
}
it open window, where user need line "not restriction". 它打开了一个窗口,用户需要在这里“没有限制”行。 And you need check is miui firmware on device or something else, for instance, like this: 您需要检查设备或其他设备上的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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.