Monday, 22 April 2013

BroadcastReceiver for Screen On/Off in ANDROID

If wanted to call a broadcast for screen on/off you can get the code sniffet from here.
Here I have used dynamic calling of Broadcast and the java source code is given below.

get more details about broadcast from :
in java file:
 
public class MainActivity extends Activity {
  //Create broadcast object
    BroadcastReceiver mybroadcast = new BroadcastReceiver() {
  
  //When Event is published, onReceive method is called
  @Override
  public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub
       Log.i("[BroadcastReceiver]", "MyReceiver");

          if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
              Log.i("[BroadcastReceiver]", "Screen ON");
          }
          else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
              Log.i("[BroadcastReceiver]", "Screen OFF");
          }
   }};

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
 
       registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
       registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
   }  
}

3 comments:

  1. Replies
    1. The code is clear and it will work as you off and on the screen.

      Delete
  2. How will this particular code work?

    ReplyDelete