Új hozzászólás Aktív témák

  • thiclyoon
    aktív tag

    Ezzel görcsölök egész délután, de nem megy. Nem hívódik meg az AirPlan.onReceive.
    Szinte triviálisan egyszerű kód, de mégsem. Hiába változtatom a repül/nem repül módot.
    Mi lehet itt még buktató?

    <receiver android:name=".AirPlan">
    <intent-filter>
    <action android:name="android.intent.action.AIRPLANE_MODE" />
    </intent-filter>
    </receiver>

    A kód jó :) valószínűleg a gond az android api miatt van: "If your app targets Android 8.0 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don't target your app specifically)" [link]

    Ha a kódot így szeretnéd ellenőrizni, akkor a module szintű build.gradle targetSdkVersion-jét vedd kisebbre (pl. 22), és egy ugyanilyen api-val rendelkező emulátoron teszeld. Viszont éles projekt, stb. esetén tedd is vissza a target-et, és próbáld meg kódból (manifest-ből töröld a kódodat):
    val intentFilter = IntentFilter("android.intent.action.AIRPLANE_MODE")
    val receiver: BroadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
    Toast.makeText(this@MainActivity, "changed", Toast.LENGTH_SHORT).show()
    }
    }
    this.registerReceiver(receiver, intentFilter)

Új hozzászólás Aktív témák