I have scheduled alarm for my application.
I have implemented broadcast receiver to be triggered once the alarm time reaches.
How to manually call broadcast receiver to execute the code inside of onReceive method without replicating the code twice.
I thought of having the code in utility singleton call and call that method by having util class instance from anywhere.
But is that any other way to call that onReceive method directly or else broadcast intent problematically.
android:exported="false" //Additional parameter of receiver when defining in manifest file.
Another question is what is that exported parameter means. Please help me to understand this.
Fire
BroadcastReceiver
usingsendBroadcast
same action which added inAndroidManifest.xml
:As in android:exported doc : Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not
Means if:
android:exported=true: other application also able to fire this broadcast receiver using action
android:exported=false: other application not able to fire this broadcast receiver using action
You need to mention the
action
which is required to be filter by Android OS to notify you. i.e.: inside manifest file,and
whenever you want to call broadcast receiver's onReceive method,
Here is a more type-safe solution:
AndroidManifest.xml
:CustomBroadcastReceiver.java
*.java
1. The way to launch a
BroadcastReceiver
manually is by callingwhere
"com.myapp.mycustomaction"
is the action specified for yourBroadcastReceiver
in the manifest. This can be called from anActivity
or aService
.2. It is known that Android allows applications to use components of other applications. In this way,
Activity
s,Service
s,BroadcastReceiver
s andContentProvider
s of my application can be started by external applications, provided that the attributeandroid:exported = true
is set in the manifest. If it is set toandroid:exported = false
, then this component cannot be started by an external application. See here.