Broadcast receiver invoking on every reboot - android

I have registered a broadcast Receiver with which should wake the application up on every reboot,
receiver android:name=".Receiver.NewLocationReceiver"
<intent-filter>
<category android:name="android.intent.category.HOME" />
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="YouWillNeverKillMe" >
</action>
</intent-filter>
</receiver>
Still the BOOT_COMPLETE filter if failing to do its job, Unable to tackle a particular scenario. Is there anything I need to add so that I can completely avoid this situation.

Popular pitfalls:
BOOT_COMPLETED is delivered to all relevant (registered) broadcast receivers only upon unlocking the device.
It is delivered in some order, so you may be last in that list and will take some time.
Make sure you have the presmission <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Related

Android device reboot/restart sends notifications immediately regardless of time

In my AndroidManifest.xml, I added an intent-filter to my receiver so that my notifications are still being sent even if the user reboots/restarts their device.
<receiver
android:name="NotificationAlarmReceiver"
android:directBootAware="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
This works, but my problem is once the device has been rebooted/restarted, it immediately sends the notification regardless of the hour and minute that were set for the notifications.
I don't want my notifications to be immediately sent. I want them to be activated again since a reboot/restart deletes all notifications that were setup on the user's device and only send notifications at the hour and minute that they were set to. How do I do that?

Detect system reboot from inside a service

Is it possible to detect when a service is stopped or destroyed by one of its own methods? e.g.
onDestroy() (not currently working)
What I need is to detect, from inside the service, whenever the device is rebooted or turned off.
You could create a BroadcastReceiver to listen for the ACTION_SHUTDOWN and QUICKBOOT_POWEROFF intents:
<receiver android:name=".PowerOffReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
Conversely you can listen for the BOOT_COMPLETED intent for when the phone is back on:
<receiver android:name=".PowerOnReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Keep in mind the latter requires the device to be unlocked by the user first. In newer API, you can listen for LOCKED_BOOT_COMPLETED which will allow you to start your service or do whatever you need before the device is unlocked.

I can't receive ACTION_MEDIA_SCANNER_FINISHED intent

I want to receive the ACTION_MEDIA_SCANNER_FINISHED, but it doesn't work.
Below is my manifest about receiver.
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_SCANNER_FINISHED" />
<data android:scheme="file" />
</intent-filter>
</receiver>
I've browsed through many related articles, but there is no correct answer and most of them are quite old.
I wonder whether it is related with API level or device or system app?

Broadcast Receiver - is using one better than many?

Here's my current setting in my android manifest:
<receiver
android:name=".receivers.BaseReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
<receiver
android:name=".receivers.BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
I've separeted those receivers because the packages only work with <data android:scheme="package" /> and the boot receiver stops working if I add thise schema to my filter.
Now my question:
Does that consume more battery? Or does it matter if I summarize the package receivers in one? Or can I just create as many receivers as I want as android will not run anything and just checks the manifests of all installed apps on demand?
Receivers are handled by the system. That is the system that send a message when the action happened so you can as many receivers as you want it won't affect the battery you just have to handle them properly.
And for the question one or many, it depend of the use you make of them. It will be easier to handle one in certain cases, for other cases it will be easier to have many to handle them separetly.

Android: How to get the installed App information using Broadcast receiver

I am new to android. I am using a Broadcast receiver which listens when a app is installed or removed.. When a app is installed or removed my Broadcast Receivers's onReceive(context,intent) will be called.. Now i need to get the info about the application installed or removed (Mainly the package name)..
Plz help
You can try this receiver and permission. (But this seem only work in /system/app)^^"
<receiver
android:name="com.your.receiver"
android:enabled="true"
android:exported="true" >
<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>
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
All the information you want is in the Intent extras.
Look at How to find the package name which has been uninstalled when using Intent.ACTION_PACKAGE_REMOVED

Categories

Resources