Search code examples
javaandroidandroid-intentbroadcastreceiver

Broadcast receiver not working when app is closed


So I have two different apps made, one sends a broadcast and another receives it and displays a toast. However, when I close the receiver app the broadcast is no longer received by the second app even though I defined the receiver in the manifest file.

The broadcast sender in the MainActivity of app1.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b = (Button)findViewById(R.id.button2);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            i.setAction("com.example.ali.rrr");
            i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            sendBroadcast(i);
            Log.e("Broadcast","sent");
        }
    });
}

App 2 broadcast receiver:

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    Toast.makeText(context, "Broadcast has been recieved!", Toast.LENGTH_SHORT).show();
    Log.e("SUCCESS", "IN RECIEVER");
    //throw new UnsupportedOperationException("Not yet implemented");
}

App 2s Manifest:

<?xml version="1.0" encoding="utf-8"?>

<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">
    <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.example.ali.rrr" />
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".Main2Activity"
        android:label="@string/title_activity_main2"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


Solution

  • First of all you need to use the Service for this functionality to work.

    In the Activity you can start and stop the service by using the below codes.

    // to start a service
    Intent service = new Intent(context, MyBrodcastRecieverService.class);
    context.startService(service);
    
    // to Stop service
    Intent service = new Intent(context, MyBrodcastRecieverService.class);
    context.stopService(service);
    

    Then you can use the below service

    public class MyBrodcastRecieverService extends Service
    {
        private static BroadcastReceiver br_ScreenOffReceiver;
    
        @Override
        public IBinder onBind(Intent arg0)
        {
            return null;
        }
    
        @Override
        public void onCreate()
        {
            registerScreenOffReceiver();
        }
    
        @Override
        public void onDestroy()
        {
            unregisterReceiver(br__ScreenOffReceiver);
            m_ScreenOffReceiver = null;
        }
    
        private void registerScreenOffReceiver()
        {
            br_ScreenOffReceiver = new BroadcastReceiver()
            {
                @Override
                public void onReceive(Context context, Intent intent)
                {
                    Log.d(TAG, "ACTION_SCREEN_OFF");
                    // do something, e.g. send Intent to main app
                }
            };
            
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
            registerReceiver(br_ScreenOffReceiver, filter);
        }
    }