Android Keep Broadcast Receiver Running After Application Exit

In some cases, you may need to create a broadcast receiver that can still run in the background after the android app exit. This example will just tell you how to do that use a broadcast receiver and android service object.

1. Screen On Off Broadcast Receiver.

  1. Below java files are the example project used files.
    D:\WORK\DEV2QA.COM-EXAMPLE-CODE\ANDROIDEXAMPLEPROJECT\EXAMPLE\APP\SRC\MAIN\JAVA\COM\DEV2QA\EXAMPLE\BROADCAST
    ├─activity
    │  └─forceoffline
    │          ScreenOnOffActivity.java
    │
    ├─receiver
    │  └─forceoffline
    │          ScreenOnOffReceiver.java
    │
    └─service
        └─forceoffline
               ScreenOnOffBackgroundService.java
  2. First, we will create a broadcast receiver that can listen and process the android screen on/off broadcast event as below.
  3. As you can see, we will check the broadcast action value and log debug message based on that value in the broadcast receiver’s onReceive() method.
  4. ScreenOnOffReceiver.java
    package com.dev2qa.example.broadcast.receiver;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    public class ScreenOnOffReceiver extends BroadcastReceiver {
    
        private final static String SCREEN_TOGGLE_TAG = "SCREEN_TOGGLE_TAG";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(Intent.ACTION_SCREEN_OFF.equals(action))
            {
                Log.d(SCREEN_TOGGLE_TAG, "Screen is turn off.");
            }else if(Intent.ACTION_SCREEN_ON.equals(action))
            {
                Log.d(SCREEN_TOGGLE_TAG, "Screen is turn on.");
            }
        }
    }

2. Register And Unregister ScreenOnOffReceiver In Activity.

  1. Now we will create an activity and register the ScreenOnOffReceiver object in it’s onCreate() method, and unregister the receiver in it’s onDestroy() method as below.
  2. ScreenOnOffActivity.java
    package com.dev2qa.example.broadcast.activity;
    
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    
    import com.dev2qa.example.R;
    import com.dev2qa.example.broadcast.receiver.ScreenOnOffReceiver;
    
    public class ScreenOnOffActivity extends AppCompatActivity {
    
        private ScreenOnOffReceiver screenOnOffReceiver = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_screen_on_off);
    
            setTitle("dev2qa.com - Keep BroadcastReceiver Running After App Exit.");
    
            // Create an IntentFilter instance.
            IntentFilter intentFilter = new IntentFilter();
    
            // Add network connectivity change action.
            intentFilter.addAction("android.intent.action.SCREEN_ON");
            intentFilter.addAction("android.intent.action.SCREEN_OFF");
    
            // Set broadcast receiver priority.
            intentFilter.setPriority(100);
    
            // Create a network change broadcast receiver.
            screenOnOffReceiver = new ScreenOnOffReceiver();
    
            // Register the broadcast receiver with the intent filter object.
            registerReceiver(screenOnOffReceiver, intentFilter);
    
            Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "onCreate: screenOnOffReceiver is registered.");
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
    
            // Unregister screenOnOffReceiver when destroy.
            if(screenOnOffReceiver!=null)
            {
                unregisterReceiver(screenOnOffReceiver);
                Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "onDestroy: screenOnOffReceiver is unregistered.");
            }
        }
    }
  3. Below is the layout XML file for the above activity.
  4. activity_screen_on_off.xml
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="Press power button to turn on / off screen and see log message in android monitor."
        android:textSize="20dp"
        android:layout_marginTop="20dp"/>
  5. Run the above activity in the below steps.
  6. Start the activity, there is a log message which said the broadcast receiver has been registered in the activity’s onCreate() method.
  7. Press the power button to turn off the screen.
  8. Press the power button again to turn on the screen.
  9. You can see log data in the android monitor console for the above steps.
  10. Type the back menu to exit the activity. You can see the broadcast receiver is unregistered in the activity’s onDestroy() method also.
  11. Press the power button to execute steps 2, 3 again, but there is not any log data printed in the android monitor console.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/BiqbgHa3ciQ

3. Register And Unregister Broadcast Receiver In Android Background Service.

  1. When you register the broadcast receiver in the android activity class, it will be stopped after the activity exit.
  2. To resolve this problem, we will create an android service object, and register and unregister the broadcast receiver in the service object.
  3. Because the android service object will still run in the background after the activity exit, so the broadcast receiver will still run also after the android app exit.

3.1 Create Android Service Class.

3.1.1 Create A Java Class That Extends android.app.Service.
  1. ScreenOnOffBackgroundService.java
    package com.dev2qa.example.broadcast.service;
    
    import android.app.Service;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.IBinder;
    import android.support.annotation.Nullable;
    import android.util.Log;
    
    import com.dev2qa.example.broadcast.receiver.ScreenOnOffReceiver;
    
    public class ScreenOnOffBackgroundService extends Service {
    
        private ScreenOnOffReceiver screenOnOffReceiver = null;
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            // Create an IntentFilter instance.
            IntentFilter intentFilter = new IntentFilter();
    
            // Add network connectivity change action.
            intentFilter.addAction("android.intent.action.SCREEN_ON");
            intentFilter.addAction("android.intent.action.SCREEN_OFF");
    
            // Set broadcast receiver priority.
            intentFilter.setPriority(100);
    
            // Create a network change broadcast receiver.
            screenOnOffReceiver = new ScreenOnOffReceiver();
    
            // Register the broadcast receiver with the intent filter object.
            registerReceiver(screenOnOffReceiver, intentFilter);
    
            Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Service onCreate: screenOnOffReceiver is registered.");
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            // Unregister screenOnOffReceiver when destroy.
            if(screenOnOffReceiver!=null)
            {
                unregisterReceiver(screenOnOffReceiver);
                Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Service onDestroy: screenOnOffReceiver is unregistered.");
            }
        }
    }
3.1.2 Add Service Xml Tag In AndroidManifest.xml File.
  1. AndroidManifest.xml
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.dev2qa.example">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            
            <activity android:name=".broadcast.activity.ScreenOnOffActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <service android:enabled="true" android:name=".broadcast.service.ScreenOnOffBackgroundService" />
        </application>
    
    </manifest>
3.1.3 Change Activity Java Code To Below.
  1. Please notice the java code that starts the service object.
    Intent backgroundService = new Intent(getApplicationContext(), ScreenOnOffBackgroundService.class);
    startService(backgroundService);
  2. ScreenOnOffActivity.java
    package com.dev2qa.example.broadcast.activity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    
    import com.dev2qa.example.R;
    import com.dev2qa.example.broadcast.receiver.ScreenOnOffReceiver;
    import com.dev2qa.example.broadcast.service.ScreenOnOffBackgroundService;
    
    public class ScreenOnOffActivity extends AppCompatActivity {
    
        private ScreenOnOffReceiver screenOnOffReceiver = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_screen_on_off);
    
            setTitle("dev2qa.com - Keep BroadcastReceiver Running After App Exit.");
    
            Intent backgroundService = new Intent(getApplicationContext(), ScreenOnOffBackgroundService.class);
            startService(backgroundService);
    
            Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Activity onCreate");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.d(ScreenOnOffReceiver.SCREEN_TOGGLE_TAG, "Activity onDestroy");
        }
    }
  3. Run the example again, you can see the below picture. From the android Logcat output, we can see the broadcast receiver still running after the android app exit.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/UMPkHiittgo

5 3 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

8 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
ans ali
ans ali

Broadcast not trigger when app is closed.

Osama Ibrahim
Osama Ibrahim

Thank you, its work fine for me
I used the same code to detect when the phone call finished

Abdelrahman Elshorafa
Abdelrahman Elshorafa

Thanks you a lot you help me to learn something I want to learn since months Thanks .

Insha2allah our god yhdeek

Welington da Silva
Welington da Silva

Thanks

Abhijit Thorat
Abhijit Thorat

Hello, I have done coding exactly like you and not getting any error as well. But still the control is not reaching to the receiver at all.
Please help!

Ismael
Ismael

I get

android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

using android 8.0

Any idea?

Nitish Ranjan
Nitish Ranjan

hey can i get the whole code?

8
0
Would love your thoughts, please comment.x
()
x