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

前端 未结 4 702
天命终不由人
天命终不由人 2021-02-18 21:46

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 re

4条回答
  •  野性不改
    2021-02-18 22:26

    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.

提交回复
热议问题