如何为广播接收器定义 IntentFilter
因此,我试图制作一个应用程序,在收到每条短信时都会显示一条祝酒消息。我希望应用程序仅显示 toast,并且我当前使用此代码来显示它,但在应用程序运行时出现异常错误。我正在使用这段代码,
BroadcastReceiver BR = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText( context, "testing", Toast.LENGTH_SHORT ).show();
}
};
IntentFilter filter = new IntentFilter();
registerReceiver(BR, filter);
我很确定它是意图过滤器,但我不确定。
So I am attempting to make an application that will show a toast message when every a text message is received. I want the application to only show the toast, and I currently using this code to show it, but I get an exception error when the application runs. I am using this code
BroadcastReceiver BR = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText( context, "testing", Toast.LENGTH_SHORT ).show();
}
};
IntentFilter filter = new IntentFilter();
registerReceiver(BR, filter);
I'm pretty sure it's the intent filter, but I'm not sure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在清单中输入广播接收器的条目,然后添加我们为活动添加的意图过滤器。这在运行时也是可能的。
You have to make the entry of your broadcastreceiver in manifest then add the intent filter as we add for our activities.It is possible on runtime too.
您应该创建扩展 BroadcastReceiver 的单独类
然后重写 onRecieve 方法。
在这里,AlarmMain.this 是主类,
OneShotAlarm.class 扩展了 BroadcaseReceiver :
您可以按照以下方式调用 onRecieve() :
您可以尝试以下操作,但它是可选的(将以下标记包含到 AndroidManifest.xml 中)
You should create seperate class which extends BroadcastReceiver
and then override onRecieve method.
Here, AlarmMain.this is main class and
OneShotAlarm.class which extends BroadcaseReceiver :
You can call onRecieve() as per following:
You can try following but it is optional (include following tag into AndroidManifest.xml)