Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an Android app's service run before it is launched by the user?

The app has a BroadcastReceiver that listens for a boot-complete event and starts a background service to send some data to my HTTP server.

My question is, if the app is never run by the user (only installed), will the BroadcastReceiver receive the boot event?

like image 779
Ray Avatar asked May 15 '13 05:05

Ray


People also ask

What happens when a service starts in Android?

A service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. A service is bound when an application component binds to it by calling bindService().

How is a service created Android?

Starting a service You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.

Can an Android app launch another app?

In android, we can lunch other applications using packing name. This example demonstrate about How to Launch an application from another application on Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How do I know if an app is running in the background Android?

You can detect currently foreground/background application with ActivityManager. getRunningAppProcesses() which returns a list of RunningAppProcessInfo records. To determine if your application is on the foreground check RunningAppProcessInfo.


2 Answers

Starting with android 3.1 user has to launch the application once for it to receive the boot_complete broadcast..

Following is from the official javadoc:

Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.

Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.

The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.

FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against. FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets. When neither or both of these flags is defined in an intent, the default behavior is to include filters of stopped applications in the list of potential targets.

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

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).

javadoc link

Check out this blog for more detail

like image 151
Praful Bhatnagar Avatar answered Oct 05 '22 02:10

Praful Bhatnagar


Yes, the Boot receiver is registered to listen to the boot so if you reboot your device it will fire, regardless of whether you started the app or not. Similarly, if you add NFC listeners to your manifest, then if someone swipes an NFC card the app will react. The Manifest is used by Android to react to whatever you've specified in it. It's not contingent on whether the app is running (or has ever run). Excellent question though! :)

EDIT as per the other answers and the documentation. This is not true anymore. Sorry for the confusion.

like image 26
Yevgeny Simkin Avatar answered Oct 05 '22 01:10

Yevgeny Simkin