I have some intent filters created that work fine but – I only get told what state the screen/phone is in after it changes. How do I find the current state? This is my filter –
receiver = new EventHandler();
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); // for calls
registerReceiver(receiver, filter);
I get an immediate notification for the battery state even though it hasn’t changed. Would be nice if the other intents fired a dummy notification like that.
Thanks.
You only get a return on the battery state immediately because
ACTION_BATTERY_CHANGEDis astickybroadcast and is persisted at all times.The other three are one-shot broadcast intents only sent when the ‘action’ has occurred. You will need to call methods on the various ‘manager’ classes to get an instant state.
For example,
TelephonyManagerhas agetCallState()method andPowerManagerwhich has aisScreenOn()method.I haven’t tried either myself but the docs suggest they’ll work for what you need.
See…
TelephonyManager
PowerManager