BroadcastReceiver not receiving BOOT_COMPLETED - android

I've looked around here for similiar problems, but for some reason my BroadcastReceiver never ends up receiving the android.intent.action.BOOT_COMPLETED Intent.
Here is my (relative) Android.Manifest File:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<receiver android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
And Here is the actual Receiver.
public class BootReceiver extends BroadcastReceiver {
private static final String TAG="BootReceiver";
#Override public void onReceive(Context context,Intent intent){
try{
context.startService(new Intent(context,ConnectivityListener.class));
Log.i(TAG,"Starting Service ConnectivityListener");
}catch(Exception e){
Log.e(TAG,e.toString());
}
}
}
Thanks! Any help is greatly appreciated

You can emulate all broadcast actions by connecting via adb to the device and open a device shell.
Here we go:
open console/terminal and navigating to /platform-tools
type adb shell or on linux/mac ./adb shell
in the shell type am broadcast -a android.intent.action.BOOT_COMPLETED or whatever action you want to fire
There are a bunch of nice commands coming with adb or the adb shell. Just try it
Regards
Flo
edit: oh damn, i wanted this answer as an answer on the "had to turn phone on/off every time". sorry folks

I'm posting this in the hope that it will be helpful to someone who has tried everything but still cannot get it to run on boot after installation or it used to work before and doesn't work anymore.
So assuming you have added the permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And registered your receiver:
<receiver android:name="com.example.startuptest.StartUpBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And coded your BroadcastReceiver:
public class StartUpBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.d("startuptest", "StartUpBootReceiver BOOT_COMPLETED");
...
}
}
}
Starting with Android 3.1 all applications, upon installation, are placed in a "stopped" state.(This is the same state that the application ends up in after the user force-stops the app from the Settings application.)
While in "stopped" state, the application will not run for any reason, except by a manual launch of an activity. (Meaning no BroadcastRecevier(ACTION_PACKAGE_INSTALLED, BOOT_COMPLETED etc. will be invoked, regardless of the event for which they have registered, until the user runs the app manually.)
This is a design decision by Google to prevent malware apps. Google has advocated that users should launch an activity from the launcher first, before that application can do much. Preventing BOOT_COMPLETED from being delivered until the activity is launched is a logical consequence of that argument.
Once a user runs any activity in your app once, you will receive the BOOT_COMPLETED broadcast after all future boots.
More details about this:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
http://commonsware.com/blog/2011/07/05/boot-completed-regression.html
http://devmaze.wordpress.com/2011/12/05/activating-applications/

If your app installed on external storage(SD card), you will never receive Boot Complete action. So you have to specify android:installLocation="internalOnly" in the manifest tag.

Your <uses-permission> element needs to be an immediate child of the <manifest> element, and your code listing above suggests that it is not.
Here is a sample project demonstrating the use of BOOT_COMPLETED.

Turns out the receiver wasn't in the tag of the manifest. Whoops! Thanks for your help guys! The worst part about testing this is having to keep turning off and on the phone. :P

This seems to be the forefront thread for this problem, so I wanted to add a solution for my C# colleagues. I racked my brain trying to figure out what I was doing wrong after trying everything here, to no avail. I finally figure out what was wrong, and it differs a bit from the advice here for C# Mono development. Basically, it boils down to something I've just learned the hard way. With C# DO NOT MODIFY AndroidManifest.xml manually!
See this guide for reference:
Xamarin: Working with AndroidManifest.xml
More directly for this problem, here is how you get this done.
First, in your project properties, under the Manifest Tab, there is a checkbox list for choosing the permissions you want to provide, one of which is RECEIVE_BOOT_COMPLETED. Check that to provide these permissions.
Secondly, you need to put the proper tags on your BroacastReceiver class.
[BroadcastReceiver]
[IntentFilter(new String[]{ Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.LowPriority)]
public class MyBootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
// Do your boot work here, set alarms, show toasts, whatever
}
}
The final part of [IntentFilter()] dealing with priority isn't required, it just lets other higher priority stuff get done first on boot, and is good practice if your App isn't a high priority thing.
As you'll see in the linked article, using these tags in your code will cause the AndroidManifest.xml file to be created at build time, with everything the way it should be. What I found was that when modifying the manifest manually to include the receiver tag, the system was causing it to look for the class one level too deep, thus throwing a ClassNotFound exception. It was trying to instantiate [Namespace].[Namespace].[BroadcastReceiver] which was wrong. And it was doing that because of the manual manifest edits.
Anyway, hope this helps.
Also, another quick tip with the adb tool. If you want to get an easier to read version of the log, try this:
C:\Android\platform-tools\adb logcat >> C:\log.txt
This will dump the logcat to a text file you can open and read a bit easier than in the command prompt window. Makes cut and paste of things a bit easier too.

Pertaining to some devices running Android Kitkat 4.4.4_r2/r1.
There seems to be a bug in Android that make android.intent.action.BOOT_COMPLETED no being broadcasted.
See:
BOOT FAILURE making Package Manager Service ready
In most cases this is not the answer to your problems (more likely because permissions etc), but if you are running Kitkat then you might have a look and see if this seems to be the case for you.
I had this problem and android.intent.action.BOOT_COMPLETED would simply not be broadcasted some of the times it had started up!

Other answers here already covered how to perfectly implement the Broadcast Receiver so that it'll work, however I still had problems receiving the BOOT_COMPLETED Intent until I realized the app was actually working when started from the phone/emulator by pressing on the app icon.
Whenever I start my app with the debug/run commands from Android Studio the BOOT_COMPLETED Intent won't be delivered, unless the app is opened and running.
I hope this can help someone who, like me, was struggling for hours with this problem.
Moreover, if anyone has an explanation for this behavior, I'd be really happy to know more about it.

on adding <category android:name="android.intent.category.HOME" /> this to my manifest file solve my problem and works.
<receiver android:name=".BroadCastRecieverClass">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>

Related

BroadcastReceiver doesnt receive when app is closed

I know this question is similar to many questions of BroadcastReceiver but as I read, non of them have solutions.
the tutorial of BroadcastReceiver tells it will work even app was not running in the background, my question is why I can not use it when app is not running
I tried to call broadcast from main activity, use service and ....
but non of them solved my problem.
here is my CODE:
MyReceiver java Class:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"BroadCast Trigger",Toast.LENGTH_SHORT).show();
}
}
Also MyManifest Code:
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
As I found there is no way to active BroadcastReceiver in Huawei Devices programmatically, but here is a solution to find device type and do needed action in this regard such as show an alert to user to activate it manually.
if ("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
// Do Needed Action
}
I faced with same problem on Huawey Honor with Android 7. On Sony and ZTE devices BroadcastReceiver works as expected. But on Honor it work some time and suddenly stop.
I discover, that problem not related with re-boot. I reboot device and broadcast receiver work after it. But sometimes, it stop without rebooting.
First i add my app to protected list according this solution:
"Protected Apps" setting on Huawei phones, and how to handle it
But it didn't help :(
Then, i add a fake accessibility service to my app, according to this recommendation:
Broadcast Receiver Not Working After Device Reboot in Android
And problem was solved!

Auto Start Service after booting device even app not opened atonce. Android

I am making a system app. In that I have a requirement is to run a service after boot load WITHOUT A SINGLE TIME LUNCHING THE APP.
this question is bit similar to this
System App auto starting
But it does not have any appropriate solution.
Also read that BOOT_COMPLETE_RECEIVER works only when app launched at once.
Use Broadcast Receiver for getting action after that start service from that broad cast receiver and use START_STICKY service so that if it is killed because of some priority than it's recreate and if you want to continuously run this service in background than WAKE_Lock that service and using Alarm Manager check it is runnig or not.
Set this in manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name="AutoStart"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
AutoStart class
public class AutoStart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
// Start your service here..
}
}
}
Thanks all for your effort, I finally got answer.
Solution:
As I stated my app is system app, System work even they not opened at once. Because they are not in stopped state i.e enforce after android 3.1.
Secondly If a user app wants this then Its manifest don't have any "android.intent.category.LAUNCHER" category in activity.
Also by adb you can enable your app by using this command
adb shell am broadcast -a com.example.demo.action.LAUNCH --include-stopped-packages (This is not tested)
Some good link to this:
http://droidyue.com/blog/2014/01/04/package-stop-state-since-android-3-dot-1/
Static BroadcastReceiver not Working after Installation from ADB

Not allowed to start service Intent without permission - sender does not get permissions

I'm working with Mark Murphy's excellent Commonsware books - but it's a lot to digest. I built the 'FakePlayer' app (pretends to be an mp3 player). It contains a service. As a learning experience I tried to write a trivial app (has only a button) whose click handler does:
Intent i = new Intent();
i.setAction("com.example.cwfakeplayer.MyPlayerService");
Context context = getApplicationContext();
context.startService(i);
It worked fine - the service start ok. I noticed Eclipse complaining about no permission on the service, so I updated the service's manifest by adding 2 lines, android:permissions and android:exported:
<service
android:name="MyPlayerService"
android:permission="com.example.fakeplayer.permission.MY_PLAYER_PERMISSION"
android:exported="true"
<intent-filter>
<action android:name="com.example.fakeplayer.MyPlayerService"></action>
</intent-filter>
</service>
I reloaded the player app onto the device (I'm using a Galaxy S2) using 'debug' under eclipse. It seemed to work; the starter app caused a permission exception, which I expected.
I then added to the starter app's manifest (to give it the permission):
<manifest
...
<uses-sdk ....
....
<uses-permission android:name="com.example.fakeplayer.permission.MY_PLAYER_PERMISSION" />
I reloaded the starter app onto the device (using debug under Eclipse). Still get the permission error in the starter app.
I removed both apps from the device and reinstalled (using debug...), service app first, then starter. Still get perm error.
I am working my way through the 'how to use a remote service' section of Mr. Murphy's Advanced Android book, so I realized this is not the best way perhaps to work across apps.
I did a 'adb shell dumpsys package', located the starter app, and found it had 'permissionsFixed=false' and no 'grantedPermissions' section. I take this to mean the manifest change in the starter app did not manage to get the perm added to the app. But I have no idea why. As a learning experience, it's generated only confusion so far....
Any clues greatly appreciated! Thanks!
I updated the service's manifest by adding 2 lines, android:permissions and android:exported
Technically, android:exported="true" is superfluous, as having the <intent-filter> automatically makes the <service> be exported.
I removed both apps from the device and reinstalled (using debug...), service app first, then starter. Still get perm error.
You do not show where you ever declare the custom permission with the <permission> element. In practice, if you control both apps, put the same <permission> element in both manifests, so the order of installation of your two apps no longer matters.
Try replace this in your manifest
<service android:name="com.example.fakeplayer.MyPlayerService"></service>
instead of
<service
android:name="MyPlayerService"
android:permission="com.example.fakeplayer.permission.MY_PLAYER_PERMISSION"
android:exported="true"
<intent-filter>
<action android:name="com.example.fakeplayer.MyPlayerService"></action>
</intent-filter>
</service>
If this doesn't work, kindly post out your error.

broadcastreceiver not working while phone is booting in Android

I have created a lock screen for ICS and it is placed in the frameworks and we can open applications using this. For the user effects I have started an animation when the lock screen is displayed. This animation is started using SCREEN_ON broadcastereceiver. But when the phone is booting up even though I registered broadcastereceiver it is not reaching to onReceive() and the animation is not starting. While phone bootup is taking place I thought that this broadcast is not having higher priority to execute and set the priority as high but it is also not working.
Check this out..
I also had the same problem Broadcast not invoking:
According to my knowledge the problem is with Android HoneyComb and ICS. I have tested same application on HoneyComb,ICS, Ginger Bread and Froyo. Worked perfectly for Froyo and Ginger bread but not for honeycomb or ics.
if you are just shutting down your phone and switching it on, then you might not get this broadcast. try to restart your phone. this would work.
you havent added any code in your question, so it woulld be really difficult to guess what you are missing and what probably is going wrong.
Well the way to receive system startup is as followsRegister receiver in manifest:
<receiver android:name=".StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Use the permission in manifest<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />Write the class below in your package
public class StartupReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.e("MyStrtupIntentReceiver" ,"################# onReceive() system boot");
}
}

Android - How to intercept the 'Install application' intent

OK, so not entirely sure this is possible...
But trying to write an application so that I can run some code before any of the following activities are performed.
1) APK is downloaded from web and market launches installer
2) Install button is pressed on android market
Is it possible to intercept and prompt on these events, or has Google locked that stuff down quite tightly?
This isn't an answer per se, but I can't find any commenting tool here. Sorry.
I'm having this issue as well. I would like to be able to detect new application installs. I know it is possible - for example, the app Apps to SD posts a notification when you install a new app that when clicked opens a dialog to move that new app to the sd card.
So far, all I've been able to figure is like this:
manifest.xml:
...
<receiver android:name=".IntentReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
...
IntentReciever.java:
public class IntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, YourService.class));
}
}
YourService is then created and calls onCreate() then onStartCommand(). However, I haven't been able to debug this or successfully display any notifications from the service class, so I'm not entirely sure this works. I have gotten this to work for other Receivers like android.intent.action.BOOT_COMPLETED.
Using a BroadcastReceiver you can filter the android.intent.action.PACKAGE_ADDED intent. However this will only be after the two actions you describe, not before. And it will not stop or interrupt the installation.
AFAIK there is no way to do anything before or to interrupt the Market. And then we're even talking about another app than the one that's being installed ofcourse.

Categories

Resources