Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to trigger a Broadcast Receiver to call its onReceive() method?

Tags:

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.

like image 892
M Vignesh Avatar asked Feb 23 '15 09:02

M Vignesh


People also ask

What is the role of the onReceive () method in the BroadcastReceiver?

onReceive. This method is called when the BroadcastReceiver is receiving an Intent broadcast. During this time you can use the other methods on BroadcastReceiver to view/modify the current result values.

How do I start a broadcast receiver?

Creating a BroadcastReceiver The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context.

What method is used to send a broadcast event in android?

The sendBroadcast(Intent) method sends broadcasts to all receivers in an undefined order. This is called a Normal Broadcast. This is more efficient, but means that receivers cannot read results from other receivers, propagate data received from the broadcast, or abort the broadcast.


1 Answers

1. The way to launch a BroadcastReceiver manually is by calling

Intent intent = new Intent("com.myapp.mycustomaction"); sendBroadcast(intent); 

where "com.myapp.mycustomaction" is the action specified for your BroadcastReceiver in the manifest. This can be called from an Activity or a Service.

2. It is known that Android allows applications to use components of other applications. In this way, Activitys, Services, BroadcastReceivers and ContentProviders of my application can be started by external applications, provided that the attribute android:exported = true is set in the manifest. If it is set to android:exported = false, then this component cannot be started by an external application. See here.

like image 146
Y.S Avatar answered Oct 03 '22 21:10

Y.S