Reputation: 33
I am developing custom UI for incoming call. I am almost done with this but now I want to load my custom UI activity only if screen is ON and user has a incoming call. I am doing all these stuff at BroadcastReceiver (android.intent.action.PHONE_STATE). So is it possible to get status of screen ON/OFF from the BrodcastReceiver.
I tried to follow the example http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/ but registering receiver from BroadcastReceiver is giving compile time error.
Please suggest me.
public class MyPhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new MyScreenReceiver();
registerReceiver(mReceiver, filter); //this gives error "The method registerReceiver(BroadcastReceiver, IntentFilter) is undefined for the type MyPhoneReceiver"
}
}
Manifest.xml
<receiver android:name="MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
Thanks
Upvotes: 0
Views: 1708
Reputation: 1037
registerReceiver() is a method of Context, so you should call context.registerReceiver(mReceiver, filter);
But you could do the following:
public class MyPhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if(pm.isScreenOn())
{
// load your UI
}
}
}
Upvotes: 1
Reputation: 2528
Here is solution for your question..
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// DO WHATEVER YOU NEED TO DO HERE
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// AND DO WHATEVER YOU NEED TO DO HERE
wasScreenOn = true;
}
}
Upvotes: 0