Android GPS状态接收器2周前停止工作
我使用了这个Android GPS状态接收器2周前停止工作,android,kotlin,broadcastreceiver,android-permissions,android-gps,Android,Kotlin,Broadcastreceiver,Android Permissions,Android Gps,我使用了这个广播接收器作为GPS状态接收器来监控用户在顶部导航菜单中打开/关闭位置的时间。它在两周前突然停止工作(未调用整个接收器onReceive()方法)(可能与Android 10版本有关)。你知道会出什么问题吗 它以前工作得很好 class GPSReceiver: BroadcastReceiver(){ companion object{ const val GPS_PAYLOAD = "gps_payload" const val GPS_
广播接收器
作为GPS状态接收器来监控用户在顶部导航菜单中打开/关闭位置的时间。它在两周前突然停止工作(未调用整个接收器onReceive()
方法)(可能与Android 10版本有关)。你知道会出什么问题吗
它以前工作得很好
class GPSReceiver: BroadcastReceiver(){
companion object{
const val GPS_PAYLOAD = "gps_payload"
const val GPS_STATE = "gps_state"
}
override fun onReceive(context: Context, intent: Intent) {
App.log("IsGPSEnabled: callingonReceive")
val action = intent.action
if(action != null && action == LocationManager.PROVIDERS_CHANGED_ACTION){
try {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val int = Intent(GPS_PAYLOAD)
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
int.putExtra(GPS_STATE, true)
} else {
int.putExtra(GPS_STATE, false)
}
LocalBroadcastManager.getInstance(context).sendBroadcast(int)
} catch (ex: Exception) {
App.log("IsGPSEnabled: $ex")
}
}
}
}
AndroidManifest:
<!-- GPS status receiver -->
<receiver android:name=".services.GPSReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Android 8.0+清单接收器已过时
正在活动中注册对象广播接收器监视意图:
registerReceiver(gpsStatusReceiver,IntentFilter(“android.location.PROVIDERS\u CHANGED”)
您必须像这样更新xml部分:
<receiver
android:name=".Util.GpsReceiver"
android:enabled="true"> <!-- do this and try again -->
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
不要忘记以下权限:
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
根据您的问题,我知道您需要在GPS打开/关闭时执行一些操作??是的,这是正确的。可能与文档列表相关。可能的替代方案,例如在应用程序运行时在“后台”监听位置状态的前台服务