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

后端 未结 3 1790
夕颜
夕颜 2021-01-03 15:55

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 i

相关标签:
3条回答
  • 2021-01-03 16:34

    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.

    0 讨论(0)
  • 2021-01-03 16:35

    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.

    0 讨论(0)
  • 2021-01-03 16:38

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

    0 讨论(0)
提交回复
热议问题