Sanat Chandravanshi
Sanat Chandravanshi

Reputation: 665

BroadcastReceiver for Screen On/Off in ANDROID When App is not running

I want to detect screen off and on event for that i have created BroadcastReceiver, It only works fine when app is running and when i remove the app from task manager then it does not works.

Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try{
        BroadcastReceiver myrcver  = new BootReceiver();
        IntentFilter screenStateFilter = new IntentFilter();
        screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
        screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(myrcver, screenStateFilter);
    }
    catch (Exception e){
        e.fillInStackTrace();
    }
}

Menifest:

<receiver
        android:name=".BootReceiver"
        android:enabled="true"
        android:exported="true"
        android:label="BootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.SCREEN_OFF" />
            <action android:name="android.intent.action.SCREEN_ON" />
        </intent-filter>
    </receiver>

Broadcast Reciever:

public class BootReceiver extends BroadcastReceiver {
boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {
    try {
        if ("android.intent.action.SCREEN_ON".equals(intent.getAction())) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                screenOff = true;
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                screenOff = false;
            }
            Intent i = new Intent(context, UpdateService.class);
            i.putExtra("screen_state", screenOff);
            context.startService(i);
        }
    }
    catch (Exception e){
        e.fillInStackTrace();
    }
}

}

Help is appreciate in advance.

Upvotes: 6

Views: 10264

Answers (3)

bhanu kaushik
bhanu kaushik

Reputation: 391

A bit late for the party but might help someone. Do as mentioned by @CookieMonster above, further while starting your service ScreenOnOffService , do like this

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(new Intent(this, ScreenOnOffService.class));
    } else {
        startService(new Intent(this, ScreenOnOffService.class));
    }

and in your ScreenOnOffService onCreate() method paste the below code

if (Build.VERSION.SDK_INT >= 26) {
        String CHANNEL_ID = "your_channel_id";
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                "Notification Channel Title",
                NotificationManager.IMPORTANCE_DEFAULT);

        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("")
                .setContentText("").build();

        startForeground(1, notification);
    }

now run your application. It will work as expected.

Upvotes: 4

vegeto
vegeto

Reputation: 47

Broad cast reciever can run when app is not running only if you add permission wake.lock in your manifest

Upvotes: -2

CookieMonster
CookieMonster

Reputation: 1833

If you want it to run in the background even if the app is not running, you will have to create a Service. In the Service class you will have to register to your Receiver.

public class ScreenOnOffService extends Service {

    private ScreenOnOffReceiver mScreenReceiver;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        registerScreenStatusReceiver();
    }

    @Override
    public void onDestroy() {
        unregisterScreenStatusReceiver();
    }

    private void registerScreenStatusReceiver() {
        mScreenReceiver = new ScreenOnOffReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        registerReceiver(mScreenReceiver, filter);
    }

    private void unregisterScreenStatusReceiver() {
        try {
            if (mScreenReceiver != null) {
                unregisterReceiver(mScreenReceiver);
            }
        } catch (IllegalArgumentException e) {}
    }
}

After creating the service class don't forget to add your service to the Manifest:

<service android:name="com.myapp.services.ScreenOnOffService" />

Then to start the service you can write this in your main activity:

Intent intent = new Intent(this, ScreenOnOffService.class);
startService(intent);

For the broadcast class:

public class ScreenOnOffReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.d("StackOverflow", "Screen Off");

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.d("StackOverflow", "Screen On");
        }
    }
}

Note: for me the screen off and screen on ,did NOT work inside the manifest , but only at runtime.

Upvotes: 7

Related Questions