Broadcast Receiver - is using one better than many? - android

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.

Related

Broadcast receiver invoking on every reboot

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" />

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?

prevent other apps from sending broadcasts to my broadcastReceiver

I have a broadcastReceiver registered in manifest that receives broadcasts sent from one of my services with a custom action. I have it already working but for security reasons i want to prevent other apps from sending fake broadcast to my receiver. How can i do that?
Manifest
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="MyAction"/>
</intent-filter>
</receiver>
Every reciever with exported tag set to false will only receive broadcasts sent from its own application process.
so it will be:
<receiver android:name=".MyReceiver"
android:exported="false">
<intent-filter>
<action android:name="MyAction"/>
</intent-filter>
</receiver>
As another solution i found that i can use permissions.
more on here

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

How to know Application Installation is completed in android?

I want to create such a service which start when any new application is installing on android device. so how to know any application getting installed in android device, is there any Intent event is fired ?
<receiver android:name="r1">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/> <data android:scheme="package"/>
</intent-filter>
</receiver>
you can try this broadcast receiver....
also go through
"android.intent.action.PACKAGE_REMOVED"
"android.intent.action.PACKAGE_REPLACED"
might come in handy...

Categories

Resources