Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get broadcast for screen lock in android

How to get trigger that screen is locked or on in android??

i tried using SCREEN_OFF & SCREEN_ON action in broadcast receiver but it's not working.

public void onReceive(Context context, Intent intent) {
    Log.d("XYZ", "Screen ON/OFF");

    Toast.makeText(context, "screen",10000).show();
    if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        .......
    }
}

in activity i have registered broadcast like-

screen is object of my broadcast receiver

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mContext.registerReceiver(screen, filter);
like image 804
yuva ツ Avatar asked Nov 11 '13 05:11

yuva ツ


People also ask

Can you lock screen while watching movie?

The lock setting is available on Android devices, iPhones, and iPads. To lock the screen while watching Netflix on your mobile device: Tap on a TV show or movie while it is playing. Tap the lock icon to lock or unlock your screen.

What is Android broadcast?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.


2 Answers

Call the UpdateService.class within your MainActivity.class .

startService(new Intent(MainActivity.this, UpdateService.class));

UpdateService.class

public class UpdateService extends Service {

    BroadcastReceiver mReceiver;
    public static int countOn = 0;
    public static int countOff = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        // register receiver that handles screen on and screen off logic
        Log.i("UpdateService", "Started");
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_ANSWER);
        mReceiver = new MyReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onDestroy() {

        unregisterReceiver(mReceiver);
        Log.i("onDestroy Reciever", "Called");

        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
            Log.i("screenON", "Called");
            Log.i("viaService", "CountOn =" + countOn);

            Toast.makeText(getApplicationContext(), "Awake", Toast.LENGTH_LONG)
                    .show();
        } else {
            Log.i("screenOFF", "Called");
            Log.i("viaService", "CountOff =" + countOff);
        }


        return START_STICKY;
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

Receiver class

public class MyReceiver extends BroadcastReceiver {
    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
            // Log.i("via Receiver","Normal ScreenOFF" );
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        } else if(intent.getAction().equals(Intent.ACTION_ANSWER)) {

        }

        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}
like image 55
Amresh Avatar answered Sep 22 '22 10:09

Amresh


Hey try using dynamic calling of broadcast,I tried this it will surly work...

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));
     }
    }
like image 33
Sanket Shah Avatar answered Sep 21 '22 10:09

Sanket Shah