2

May I know if a statically-declared BroadcastReceiver is able to receive broadcasts, even when the App has been removed from Recent Tasks?

I am trying to listen to events broadcasted when a Bluetooth device has been connected.

AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="FROM_SERVICE"/>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>
</application>

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        Log.i("MyReceiver", intent.getAction());
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

        if ( BluetoothDevice.ACTION_ACL_CONNECTED.equals(action) ) {
            Log.i("MyReceiver", "BT device connected!");
            v.vibrate(500); // Vibrate for 500 milliseconds
        }
    }
}

Reference:

Why BroadcastReceiver works even when app is in background ?

Keep broadcast receiver running after application is closed

Android BroadcastReceiver Not being called after app closed from Running App List

Community
  • 1
  • 1
Wilson
  • 55
  • 1
  • 7
  • No it will not receive broadcasts because when you remove app from recent tasks then BR will be unregistered. – Piyush May 06 '16 at 08:25
  • 1
    @PiyushGupta Then is there any other ways? I've tried Service and IntentService, which both were also killed when the App is removed from recent tasks. Are we able to do something like when the Music app is removed from recent tasks, yet it will still play the music and the controls are in the notification bar? Thanks! – Wilson May 06 '16 at 08:43
  • @Wilson have get any solution for this issue , because i am also facing same issue for some particular mobile phone. – Parth Jan 04 '17 at 11:08
  • Hi @Parth, I'm sorry but according to my current knowledge, it is not possible to do so. However, depending on your application, you could look into push notification as an alternative. – Wilson Feb 14 '17 at 07:21

1 Answers1

0

Statically declared broadcast will work even if you remove it from the recent tasks. Adding documentation from the developer site. Hope this helps!

Manifest-declared receivers If you declare a broadcast receiver in your manifest, the system launches your app (if the app is not already running) when the broadcast is sent.

Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.

Varun Ajay Gupta
  • 349
  • 1
  • 3
  • 11