2 years ago

#57191

test-img

Da_Pusti

Braodcast Receiver for API > 24

i´m working now with Broadcastreceviers. I did it on 2 ways. First with setting it in the AndroidManifest.xml and second on init the Receiver through my MainActivity.

Every time its starting to charge a Toast should pop and and every time it´s disconnected it´s also showing a Toast. I created a virtual Device "Nexus 5X API 29" and it´s not working, whatever i tried to do. Then i tried it with another API and created a "Pixel 2 API 24" and everything is working.

My question now is: Do i have to care about something, thats its working on every API or isn´t it just supported on API > 24?

    <receiver android:name=".MyBroadCastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        </intent-filter>
    </receiver>

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast

class MyBroadCastReceiver  : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        if(intent!!.action == Intent.ACTION_POWER_CONNECTED) {
            Toast.makeText(context!!,"Action Power connected",Toast.LENGTH_SHORT).show()
        }
    }

}

Kind regards

android

api

kotlin

broadcastreceiver

0 Answers

Your Answer

Accepted video resources