BroadCast Receiver in android versions greater then 2.3 - android

Well, there are lot of threads on this topic but all are before the release of android 3.1.
Now broadcast receivers will not work until user manually launches the application, i.e for broadcast receivers to work, the application should be in running state not stopped. There are certain questions in my mind right now, specially about BOOT_COMPLETED receiver.
Why would they still want to have BOOT_COMPLETED and won't let application use it? I mean there seem to be no point in having it. When system boots, apps are in stopped state and no app will receive this event if I'm not mistaking.
And on the developer page I read this:
"The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.
1:FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against.
2:FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets"
Can someone please explain the meaning of it. Can I still receive broadcasts when my app is in stopped state? And how can I register such receivers in manifest.xml ? I know these flags are added in the code but can I do similar in the manifest.xml?

Apps are in the stopped state if and only if they have never been manually launched by the user. It the user has launched the app at least once, the app can register for an receive BOOT_COMPLETE messages at startup.

Related

Android broadcast receiver to get status of installation even app is closed

Hi I am working with android.Firstly I am not familiar with broadcast receiver. I need to create an app in which, if anyone installed my app a broadcast receiver will run which check the installation status like ACTION_PACKAGE_INSTALL and android.intent.action.PACKAGE_REMOVED
.But How can i get these status even my app is closed ?? Is it possible with broadcast receiver ?? Please help me, thanks in advance :)
basically, BroadcastReceiver can receives broadcasts even if your app is not in foreground, or even if your application's process is not alive.
this can be done by declaring the receiver within your application's Manifest.xml file
when you declare a receiver in the Manifest file - then the system would wake up your application and invoke your receiver if it set with the appropriate intent filter that responding to the broadcast that been sent (in your case - ACTION_PACKAGE_INSTALL and PACKAGE_REMOVED actions..)
but unfortunately there are also bad news for you:
from Android ICS and above - you cannot receive any broadcasts until you application launched explicitly by the user at least once!
this is due to security reasons Google decided is necessary, to prevent from malicious apps doing staff entirely transparent without the user launched them at all even once..
so the answer basically is - no! you can't perform any code and receive any events from your app until it launched at least once.
by the way - you wouldn't received ACTION_PACKAGE_INSTALL and PACKAGE_REMOVED broadcasts for your own app that been installed or uninstalled anyway.
by reading your question again, honestly I'm not sure what is that you expects to happend:
it is no make any sense to "check your application installed status" if it unistalled or not. after all - if it uninstalled - then it can't run (and perform any code) anyway.

Does GCM run in background?

I have implemented GCM in 5 of my apps still I have a doubt about the running of GCM in background cause of apps behavior, may be am little confused about apps foreground and background running styles. I see my apps listed under "Downloaded" section of the Application in Settings when they get installed and registered the device id and I press the back key in my app.
They don't get listed under "Running" section. They come to "Running" section when there is a message (notification) and when the notification is generated and shown in notification bar they get cleared off from "Running". However if I click on "Downloaded" section, I see a "Force Stop" button activated (which I see for most of the apps) that means something is running.
My question is that is my app running and taking resource of cell phone while there are no activity open if I am using GCM in my app? Is there a service running in background if yes they why it is not listed with all other apps in "Running"?
Force Stopping an app doesn't mean that it was running before you force stopped it. It's just a way to let the user tell Android they don't want the app to be started again, until the user launches it again manually.
If you force stop an application, it won't be able to receive GCM messages until the user manually launches it again, because the GCM background service won't be able to launch it automatically.
This behavior was introduced in Android 3.1 with Launch Controls:
Launch controls on stopped applications
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).
GCM has a process running in the background, but it's a process that serves all the applkication on the device, so installing additional apps that use GCM has no effect on the number of background processes running.
Its like asking if the Calling App or the Message app always keep running, waiting for a phone or an SMS. Well, there are Android Framework Components called BroadCastReceivers. The OS Broadcasts an Intent message whenever a call is received, message is received, Low Battrey is detected, An Alarm is Fired and even when an Notification is received.
Then we have various applications which are registered to receive some Broadcast messages that the OS fires. This is done in the apps Manifest (generally). So if your app is registered to receive a Broadcast message for GCM message, your app will receive it.
Just remove the broadcast receiver for GCM in your Manifest. Your app will no longer respond to a GCM. Next how does the app distinguish between multiple GCM receivers on a single Device?
I mean how to distinguish if the notification GCM is for app A or app B?
This has to do with the APP_ID with which you register with GCM. That can be mapped to your App's Package name. We provide package name when we register for GCM

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

Exclude Broadcast Receiver from Recent Application List/Task Manager

I have a semi-successful app on the market that uses broadcast receivers for a few of its features. As a request from many of the users, I have successfully excluded a few activities from the "Recent Applications List" by adding this line to the activities in the manifest:
android:excludeFromRecents="true"
I am trying to do the same thing with my broadcast receiver. I have tried adding that line to my receiver and all of the dependant activities to no avail. What can I do to "hide" the broadcast receiver from the user? Is this supposed to work, or is there an alternative?
Also, how do I stop the app from showing in the task manager (running application list) when a broadcast is received? There are quite a few apps on my phone (my app included) that show up in my 3rd party task manager that do not show as running applications in the default settings->apps->running list. Can somebody please explain why this is the case, and what I can possibly do to keep it off the task manager list as well (without knowing how the each individual task manager searches for apps)?
Thanks!
NOTE: I use the word "hide" apprehensively. It is basically to de-clutter the recent applications list as requested by the users. No ill intents :D.
When you receive the broadcast start/launch the new activity/service with FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS. It's hard to tell without the source code, but you may also want to add FLAG_ACTIVITY_NO_HISTORY.

Android: do BroadcastReceivers stop if an app is killed?

If a declare a BroadcastReceiver in AndroidManifest.xml, the reciever works, as it should, even on device boot when my application hasn't started yet, but if I force my app to stop from Settings the receiver seems to break down too.
Can it be that "Force stop" in Android 2.2 also makes some cleanup after the application (including BroadcastReceivers or maybe alarms set by the app in AlarmManager which should broadcast the intents I receive)?
By the way, how can I see in Eclipse all broadcasts being sent in the device?
Psycho,
Force Stop should not be used to attempt to test your app from a "non-running" state. I would say the behavior is "undefined" at best. It is not uncommon that after using Force Stop on an app, that you must manually restart it to get ANY of its usability back (including BroadcastReceiver). If your app is able to receive BroadcastReceiverevents including the BOOT_COMPLETE Broadcast than you shouldn't really need to test it further.
I believe the intended purpose of Force Stop was to completely stop an annoying app's functionality. If an app is running in the background often because its receiving a lot of broadcasts and restarting, wouldn't you think Force Stop should prevent that behavior until the app is manually restarted by the user?
Also, I don't believe there is a way to view Broadcast events from Eclipse.
In eclipse there is no way to see the "broadcast is sent"
Also If you have registered the Broadcast in manifest for which you want to receive the event then system will call onReceived method

Categories

Resources