I've recently added GCM messaging to my app using google's helper classes (GCMBroadcastReceiver, GCMBaseIntentService). It works beautifully when the app is running, both when it's in the foreground and when it's not. However, when it's not running, nothing works.
As a test, I extended GCMBroadcastReceiver and added log statements to getGCMIntentServiceClassName() and peekService(). When the app is running and a message arrives I see the former called. The OS then instantiates my service class, which eventually results in onMessage() being called.
When the app isn't running getGCMIntentServiceClassName() never gets called.
My manifest is pretty much the boiler-plate code from Google's GCM examples.
Is there an extra permission or flag I need to set in order for the OS to wake up my app when it's not running and a message arrives w/ the correct intent category? My receiver is defined as:
<receiver
android:name=".GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="PACKAGENAME" />
</intent-filter>
</receiver>
Bear in mind: this works when the app is running in the background.
Bleh. Figured it out. David Wasser's answer here:
BroadcastReceiver isn't working
explains why I'm not seeing the broadcasts when my app isn't running. I was force quitting it from Manage Applications, which puts it into the "stopped" state (and thereby causes the system to exclude it from broadcasts by default).
When I install the app, launch it, power down the device, then power it on again, I'm receiving the broadcasts properly.
I had same issue and fixed by just running the through RUN button from Android Studio.
I think in Debug Mode it doesn't work
Related
There is a kiosk app called Mobilock. This app starts way faster (Almost 5 seconds before) than my own app which starts with BOOT_COMPLETED broadcast.
My own app has the highest priority which is max value of integer. So this is not about the priority.
These guys have found a way to start their application 5 second sooner than BOOT_COMPLETED broadcast.
Has anyone got an idea about what they are doing?
Oh my god! I've luckily found it. :)
This Page Says : Apps must register their components with the system before they can run during Direct Boot mode or access device encrypted storage. Apps register with the system by marking components as encryption aware. To mark your component as encryption aware, set the android:directBootAware attribute to true in your manifest.
Encryption aware components can register to receive a ACTION_LOCKED_BOOT_COMPLETED broadcast message from the system when the device has been restarted. At this point device encrypted storage is available, and your component can execute tasks that need to be run during Direct Boot mode, such as triggering a scheduled alarm.
You just need to put
android:directBootAware="true"
So the code in manifest is;
<receiver
android:directBootAware="true" >
...
<intent-filter>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
</intent-filter>
</receiver>
Listen also to android.intent.action.QUICKBOOT_POWERON and android.intent.action.LOCKED_BOOT_COMPLETED.
It seems to be device-dependant, which broadcast is sent first.
I was expecting my app to load the BroadcastReceiver AutoStartOnBoot when I reboot my device.
I uninstall and install the app. Which means that all existing settings are deleted. I then power down the phone. And power it back up, the Broadcast receiver is never called.
I now, power down the device one more time and power it up again. Yet, broadcast receiver is not called.
I now launch the app once. Clear data. And power it down. I power it up. Now, the broadcast receiver is called.
Manifest
<receiver
android:name=".AutoStartOnBoot"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
I have the permission setup
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Edit:
From your First two Points
1.I uninstall and install the app. Which means that all existing settings are deleted. I then power down the phone. And power it back up, the Broadcast receiver is never called.
2.I now, power down the device one more time and power it up again. Yet, broadcast receiver is not called.
Why it is not working??
Here your just installed the app but not launched.In android after first launch only all your manifest data is registered and all receivers work.But in your third case its working because you are launched the app so here all receivers gets registered.
For more check here Broadcast Receiver not working immediately after package installation
You have to add this permission outside of the <application> tag in manifest file
instead of this
<android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
Add this like
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
The behavior that you describe is perfectly normal. Something needs to use an explicit Intent to start up one of your components before your manifest-registered receivers will work. Typically, that will be the user tapping on your home screen launcher icon.
See the Android 3.1 release notes for more about this.
All answers are correct. This behavior is as per expectation. The app is inactive when installed, until its manually launched by the owner. Only after that is the BOOT_COMPLETED broadcast receiver registered to the OS.
Unless we put the app in the system folder, which keeps all apps in active state. We are device company, adb push your.apk /system/app is possible for us.
Some interesting links, here and here
In my application, there's a feature that allows users to dial a specific number and brings up an activity to front. I have the following receiver, and the only receiver registered in AndroidManifest.xml.
<receiver android:name="com.example.myapp.OutgoingCallListener" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Please note there's no BOOT_COMPLETED intent or service.
Now here's the thing I couldn't figure out. When I reboot my device, go check the Running Apps, my application is not listed there. But, if I dial the specific number, my application starts and the activity is brought to front.
My question is: If the app is not a service, and not started on boot, how could it recieve intent from Android? That is, in my case, how could my app listen to NEW_OUTGOING_CALL while it's not started at all?
A BroadcastReceiver that is registered in the manifest is always capable of responding to a matching broadcast. If your process is not running for any reason, Android will start up a process for you.
I have two apps that I have complete control over. Both are signed with the same cert and both use the exact same intent filter. One sends the broadcast from a fragment, the other is suppose to receive it and do something. This however is not working:
Strings.FILTER_INIT_REGISTER = "com.app.FILTER_INIT_REGISTER"
Intent intent = new Intent(Strings.FILTER_INIT_REGISTER);
getActivity().sendBroadcast(intent);
I have registered the receiver in the Manifest app tag for the app containing the ReportingReceiver class:
<receiver
android:name=".receivers.ReportingReceiver"
android:exported="true"
>
<intent-filter>
<action android:name="com.app.FILTER_INIT_REGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Curious why the ReportingReceiver class is not getting the intent call?
If your application only has a service and receivers then this won't work in Android 3.1 and later. The reason is that the system will not send broadcast Intents to application that are in the STOPPED STATE. An application is in the STOPPED STATE when it is first installed. It is removed from the STOPPED STATE when the user manually starts the application for the first time. It is returned to the STOPPED STATE if the user forces the application to stop using the application manager tool.
Since your application has no Activities, there is no way for the user to "start" it. Therefore it will never come out of the stopped state.
See http://developer.android.com/about/versions/android-3.1.html#launchcontrols
As Android Addict says in his comment to David Wasser's answer ... there is a way around this behaviour.
Just add the following flag to the calling Intent. This will ensure that you also reach broadcast receivers from "stopped" applications.
http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES
You can read more about this Android 3.1 change here
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
and here
http://code.google.com/p/android/issues/detail?id=18225
I have implemented an app that is basically a custom app store for updating and launching a family of related apps. It also needs to update itself, which works, but the app is killed without warning during the install process. I want to automatically restart the app in this case so that the user can continue to use it immediately after an update.
So I made a separate application including only a single Broadcast Receiver that listens for package events for the first app's package name and starts a new activity. That receiver is never called:
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<receiver android:name=".AppUpdateReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
In searching for similar implementations I have seen directly contradictory information on whether an application with only a receiver will ever execute, and whether a receiver will be called if its app is not already running. I've even come across example code of an application containing only a receiver, with a manifest very similar to my own. So what do I need in this application to ensure that the receiver is called whenever another package is installed?
If there is a better solution, I'd be happy to hear it.
Depending on the version of Android, you might need to start an application component in order for the BroadcastReceiver to be registered. By this I mean there will need to be a launcher Activity which must be started manually by the user.
From Honeycomb (I think) onwards it isn't possible to have application components 'active' unless the app has been manually started in some way. The reasoning behind this is the potential for insecure code executing without the end-users' knowledge.
I suspect this is what you're experiencing. To test it, add a simple "Hello World" Activity to the app that has the BroadcastReceiver in it. Launch the Activity and then check to see if the BroadcastReceiver then gets called after your other package is updated.