Display screen/activity on Unlock event?
<p>I essentially want to display a screen whenever the screen is unlocked regardless of the application already running.</p>

<p>Can someone just tell me how to display text as soon as the phone gets unlocked. I can take it from then on.</p>

<p>I have the following code up till now which I found on the net....</p>

<p>Suppose I want to display <code>abc.xml</code> as soon as the phone gets unlocked. How will i add it in the ScreenReceiver Class ? </p>

<p>Also I do not want to set any screen when the application runs.Do I need to run the code below as service ?</p>

<pre><code>public class SampleActivity extends Activity {

//Declare the necessary variables
private BroadcastReceiver mReceiver;


   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

     filter.addAction(Intent.ACTION_SCREEN_OFF);
     filter.addAction(Intent.ACTION_USER_PRESENT);

     mReceiver = new ScreenReceiver();
     registerReceiver(mReceiver, filter);

   }


    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.v("$$$$$$", "In Method: onDestroy()");

        if (mReceiver != null)
        {
             unregisterReceiver(mReceiver);
             mReceiver = null;
        }          

    }

}
</code></pre>

<p>where the Screen Reciever class is as follows</p>

<pre><code>public class ScreenReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent)
   {
      if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
      {    
            Log.v("$$$$$$", "In Method:  ACTION_SCREEN_OFF");
            // onPause() will be called.
      }
      else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
      {
            Log.v("$$$$$$", "In Method:  ACTION_SCREEN_ON");
            //onResume() will be called.

            //  Better check for whether the screen was already locked
            //if locked, do not take any resuming action in onResume()

            //Suggest you, not to take any resuming action here.       
      }
      else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
      {
            Log.v("$$$$$$", "In Method:  ACTION_USER_PRESENT");
            //  Handle resuming events

      }

   }
}
</code></pre>
