M P
M P

Reputation: 113

BroadcastReceiver in activity

I want to change the KeepScreenOn value of my activity, therefore I want to use an BroadcastReceiver inside the activity. I register the receiver in onCreate with:

registerReceiver(receiver, new IntentFilter("ACTION_POWER_CONNECTED"));
            registerReceiver(receiver, new IntentFilter("ACTION_POWER_DISCONNECTED"));

and unregister it in onPause:

unregisterReceiver(receiver);

The receiver looks like this:

    private BroadcastReceiver receiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
            findViewById(R.id.fullscreen_content).setKeepScreenOn(true);
        } else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
            media=MediaPlayer.create(context,R.raw.battery);
            media.start();
            findViewById(R.id.fullscreen_content).setKeepScreenOn(false);
        }
    }
};

No errors, it simply does not change anything when disconnecting/connecting to power source. Any suggestions?

Upvotes: 1

Views: 990

Answers (3)

Jibran Khan
Jibran Khan

Reputation: 3256

Have your registered Receiver actions in the AndroidManifest.xml ?

    <receiver android:name="com.app.example.firstapp.MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_OFF"></action>
                <action android:name="android.intent.action.SCREEN_ON"></action>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
                 <action android:name="android.intent.action.ACTION_SHUTDOWN"></action>
            </intent-filter>
        </receiver>

Also do not forget to add the permission

<uses-permission android:name="android.permission.BATTERY_STATS"/>

Upvotes: 0

griffinjm
griffinjm

Reputation: 503

Firstly set debug points in your receiver to see if it's actually firing

If it is not then:

I'm not sitting at my IDE now but I think this will not work because you register the same receiver with different filters, the second will overwrite the first action.

If you want one receiver to have multiple actions you should create a filter and add multiple actions to it using the IntentFilter addAction() method. Then when your receiver is called, you should use the Intent getAction() method to figure out which action has fired.

You should also probably only register your receiver in onStart or onResume.

If it is firing:

For keeping the screen on try the solution here: Keep Screen On Flag

Upvotes: 0

Philippe Banwarth
Philippe Banwarth

Reputation: 17755

You should use the defined constants, not string values :

IntentFilter(Intent.ACTION_POWER_CONNECTED)

instead of :

IntentFilter("ACTION_POWER_CONNECTED")

Upvotes: 2

Related Questions