Detecting Screen on and Screen Off via services

Hi buddies, have a happy Sunday.

Today I'll explore about detecting screen on and screen off in android devices.

I got your mind, can use also by Broad cast Receivers. Yes, you can.

But how do you know, if app goes offline and screen off.

First write the Broad cast receiver class for receiving results.

ScreenReceiver.Java
--------------------------
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ScreenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
            Log.i("screenLog", "screen off");

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
            Log.i("screenLog", "screen on");
        }
    }
}

Then write the service class:

UpdateScreen.Java

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.appl.ex.receiver.ScreenReceiver;


public class UpdateScreen extends Service {
    BroadcastReceiver mReceiver;

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


    @Override
    public void onDestroy() {
        unregisterReceiver(mReceiver);
        Log.i("onDestroy Reciever", "Called");
        super.onDestroy();
    }


    @Override
    public void onStart(Intent intent, int startId) {
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
            Log.i("screenON", "Called");
            Toast.makeText(getApplicationContext(), "Awake", Toast.LENGTH_LONG)
                    .show();
        } else {
            Log.i("screenOFF", "Called");
            Toast.makeText(getApplicationContext(), "Sleep",
                    Toast.LENGTH_LONG)
                    .show();
        }
    }


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


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }
}
Add the permission in Manifest, like this.

<uses-permission android:name="android.permission.DEVICE_POWER" />


Inside the Application:

<service android:name=".service.UpdateScreen" />

Finally start the service in your activity:

Intent service = new Intent(DrawerActivity.this, UpdateScreen.class);
startService(service);

Comments

Popular posts from this blog

Flutter Bloc - Clean Architecture

What's new in android 14?

Dependencies vs Dev Dependencies in Flutter