Question

So I am making an app that listens for Screen On activity in android. I have a broadcast receiver that receives Intent.ACTION_SCREEN_ON. This receiver then starts an activity.

Everything works fine but it starts the activity even when the screen was on because of an incoming call.

This is my code.

if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
    Intent i = new Intent(context, MainScreenActivity.class);
    i.putExtras(intent);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);

    Log.e("receiver", "throwing in the lock screen");
}

I am not quite sure how to I check the current state of the phone. I have the permission to read PHONE_STATE but how do I find out if the sceen on action was fired because of the call?

Also there is a delay when the screen is actually turned on and the activity is displayed. The default lock screen is visible for a short time and then the custom one appears. Is there anything that can be done to avoid this delay?

Was it helpful?

Solution

Register another broadcast receiver which responds to call incoming state set one flag which you can check in your receiver Intent.ACTION_SCREEN_ON) if flag is already set then skip to starting activity and reset flag else start activity like

if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE"))
{
    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                              TelephonyManager.EXTRA_STATE_RINGING)) 
{
    incomingcall = true;
}
}

and check

if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
if(incomingcall ==true)
{
    //skip the reciver and reset flag incomingcall = false;
}
else{
Intent i = new Intent(context, MainScreenActivity.class);
i.putExtras(intent);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

Log.e("receiver", "throwing in the lock screen");
}
}

for more information about ordered broadcast http://android-developers.blogspot.in/2011/01/processing-ordered-broadcasts.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top