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

M Vignesh picture M Vignesh · Feb 23, 2015 · Viewed 33.5k times · Source

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.

Answer

Y.S picture Y.S · Feb 23, 2015

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.