0

How chat apps (e. g. Messenger) listen to incoming messages even if their activity haven't been started yet since in android 3.1 and later this is not possible:

Manifest:

<service android:name=".ManagerService" android:enabled="true" />
<receiver
    android:name=".BootFinishedReceiver"
    android:directBootAware="true"
    android:enabled="true"
    android:exported="false"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Receiver:

 public class BootFinishedReceiver extends BroadcastReceiver {
       @Override
       public void onReceive(Context context, Intent intent) {
            Intent serviceIntent = new Intent(context, ManagerService.class);
            context.startService(serviceIntent);
       }
   }

There have to be some way around as chat apps are still working this way.

Thanks for any informations or ideas

MrKew
  • 333
  • 1
  • 14

1 Answers1

0

Your question is quite open-ended and broad. But to the link that you have pointed about the broadcast receiver when the app is not running. There is a comment on the same answer that says:

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications). That means, the user should launch the app at least once after installation to activate the application, then the app can receive all implicit broadcast from OS as normal.

The app is stopped when it is just installed. As soon as you launch the app for the first time, The application can listen to broadcast receivers and can run background services even when the app is closed.

The chat applications basically implement socket.io that keeps up the communication on both ends. Furthermore, you may implement FCM to get notifications and messages even when the app is killed.

I hope you, understand the concept :).

Tarun Kumar
  • 498
  • 5
  • 16
  • Yes, I understand, I didn't read it carefully enough... Anyway I can't get BroadcastReceiver working, ON_BOOT is never received. Please see [my other question](https://stackoverflow.com/questions/51469869/obtaining-list-of-connected-bluetooth-low-energy-devices-in-android-3-1-and-late) for more details. I'm not that much interested in chat apps (but it seemed to be similar to me). – MrKew Jul 27 '18 at 14:08