Приемник состояния GPS перестал работать 2 недели назад - PullRequest
2 голосов
/ 07 октября 2019

Я использовал это BroadcastReceiver в качестве приемника состояния GPS для мониторинга, когда пользователь включает / выключает свое местоположение в верхнем меню навигации. Он внезапно перестал работать (метод целого приемника onReceive() не вызывается) 2 недели назад (вероятно, как-то связано с выпуском 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+ устарел

Регистрация объекта намерения мониторинга BroadcastReceiver в Activity:

registerReceiver(gpsStatusReceiver, IntentFilter("android.location.PROVIDERS_CHANGED"))

1 Ответ

0 голосов
/ 16 октября 2019

вы должны обновить свою 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"/>
...