Android - 如何触发广播接收器调用其 onReceive() 方法? [英] Android - How to trigger a Broadcast Receiver to call its onReceive() method?
问题描述
我已为我的应用程序安排了闹钟.
I have scheduled alarm for my application.
我已经实现了一旦到达警报时间就会触发广播接收器.
I have implemented broadcast receiver to be triggered once the alarm time reaches.
如何手动调用广播接收器来执行 onReceive 方法中的代码,而不需要两次复制代码.
How to manually call broadcast receiver to execute the code inside of onReceive method without replicating the code twice.
我想在实用程序单例调用中使用代码,并通过从任何地方使用 util 类实例来调用该方法.
I thought of having the code in utility singleton call and call that method by having util class instance from anywhere.
但是,是否有任何其他方式可以直接调用该 onReceive 方法,否则广播意图会出现问题.
But is that any other way to call that onReceive method directly or else broadcast intent problematically.
android:exported="false"//接收时的附加参数在清单文件中定义.
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.
推荐答案
1. 手动启动 BroadcastReceiver
的方式是调用
1. The way to launch a BroadcastReceiver
manually is by calling
Intent intent = new Intent("com.myapp.mycustomaction");
sendBroadcast(intent);
其中 "com.myapp.mycustomaction"
是在清单中为您的 BroadcastReceiver
指定的操作.这可以从 Activity
或 Service
调用.
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.众所周知,Android 允许应用程序使用其他应用程序的组件.这样,我的应用程序的 Activity
s、Service
s、BroadcastReceiver
s 和 ContentProvider
s 可以通过外部应用程序,前提是在清单中设置了属性 android:exported = true
.如果设置为android:exported = false
,则该组件不能被外部应用启动.请参阅此处.
2. It is known that Android allows applications to use components of other applications. In this way, Activity
s, Service
s, BroadcastReceiver
s and ContentProvider
s 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.
这篇关于Android - 如何触发广播接收器调用其 onReceive() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!