2

I essentially want to display a screen whenever the screen is unlocked regardless of the application already running.

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

I have the following code up till now which I found on the net....

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

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

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;
        }          

    }

}

where the Screen Reciever class is as follows

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

      }

   }
}
anon
  • 342
  • 7
  • 22

1 Answers1

3

For one, you don't display abc.xml, you display an activity, dialog, or other UI component. You can set up a broadcast receiver that listens for the ACTION_BOOT_COMPLETED intent. Once the device boot completes, you can start a sticky service to listen for the actions you have above. Presumably you'll want to display abc.xml in an activity, so you'll need to fire startActivity from one of the if() blocks above.

Phix
  • 9,364
  • 4
  • 35
  • 62