Android Keep Broadcast Receiver Running After Application Exit

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

1. Screen On Off Broadcast Receiver.

Below three java files in red rectangle are the example files.

android keep broadcast receiver running after application exit example java files

First we will create a broadcast receiver which can listen and process android screen on / off broadcast event as below.

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.

ScreenOnOffReceiver.java

package com.dev2qa.example.broadcast.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Jerry on 1/5/2018.
 */

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.

Now we will create an activity and register ScreenOnOffReceiver in it’s onCreate() method, and unregister the receiver in it’s onDestroy() method as below.

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.");
        }
    }
}

Below is the layout xml file for above activity.

READ :   How To Support Multiple Screen Size In Android

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"/>

Run Above Activity In Below Steps.

  1. Start the activity, there is a log message which said the broadcast receiver has been registered in the activity’s onCreate() method.
  2. Press the power button to turn off screen.
  3. Press the power button again to turn on screen.
  4. You can see log data in android monitor console for above steps.
  5. Type the back menu to exit the activity. You can see the broadcast receiver is unregistered in the activity’s onDestroy() method also.
  6. Press the power button to execute step 2 , 3 again, but there is not any log data printed in the android monitor console.

android broadcast receiver stopped after activity exit

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

When you register the broadcast receiver in activity, it will be stopped after the activity exit.

To resolve this problem, we will create an android service object, and register and unregister the broadcast receiver in the service object.

Because the android service object will still run at 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.

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;

/**
 * Created by Jerry on 1/5/2018.
 */

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.

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.

Please notice the java code that start the service object.

Intent backgroundService = new Intent(getApplicationContext(), ScreenOnOffBackgroundService.class);
startService(backgroundService);

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");
    }
}

Run the example again, you can see below picture. From the logcat output, we can see the broadcast receiver still running after the android app exit.

READ :   How To Register BroadcastReceiver In Android Activity

android run broadcast receiver in background service

8 Comments


  1. hey can i get the whole code?

    Reply

  2. 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?

    Reply

  3. 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!

    Reply

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

    Insha2allah our god yhdeek

    Reply

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

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