注销扩展类的BroadcastReceiver [英] Unregister BroadcastReceiver which extends a class
问题描述
我的班级如下:
public class FYPSmsReceiverBroadcast extends BroadcastReceiver
当我收到短信时已经存在该应用程序之后,由于出现强制关闭错误,我需要注销接收器。 (表明某些内容仍在监听短信,但由于该应用已关闭,因此无法访问特定活动;该错误似乎仅在收到1个文本后发生,而第二个文本似乎没有产生强制关闭错误)
I need to unregister the receiver as I am getting a force close error AFTER I have existed the application when I receive an SMS message. (indicating something is still listening for the sms but not able to access a particular activity because the app has been closed; the error only seems to happen after 1 text received, a second text seems to produce no force close error)
有很多类似的问题,但没有一个可以帮助我采用的方法。我尝试过添加如下所示的onPause:
There are a number of somewhat similar questions, but none that can assist the method I have employed. I have tried adding an onPause like below:
public void onPause() {
unregisterReceiver(FYPSmsReceiverBroadcast);
}
但这会导致以下日食错误:'FYPReceiverBroadcast无法解析为变量
But this results in the following error in eclipse: 'FYPReceiverBroadcast cannot be resolved to a variable'
在扩展广播接收器的类的onReceive方法中,我调用了:
FYPSpeakerActivity.speakSMSfrom();
In the onReceive method of the class that extends Broadcast receiver I have a call to: FYPSpeakerActivity.speakSMSfrom();
哪个调用了另一个使用文本进行语音转换的类-此行似乎是在收到SMS时被调用的(尽管应用程序已关闭),并产生了强制关闭错误。
Which calls another class that uses text to speech - This line appears to be the one being called (despite the application being closed) on receipt of an SMS, and creates the force close error.
有人可以建议吗?
推荐答案
FYPReceiverBroadcast
是类
。您需要传递对象
,下面是一个示例:
FYPReceiverBroadcast
is a class
. You need to pass an object
, here is an example:
FYPReceiverBroadcast myFYPReceiverBroadcast = new FYPReceiverBroadcast(...);
...
public void onPause() {
unregisterReceiver(myFYPReceiverBroadcast);
}
这是一个基本的编程概念,建议您阅读以下文章了解差异:
This is such a fundamental programming concept, I recommend you to read the following article to learn the difference:
对象是具有相关状态和
行为的软件包。类是从中创建对象的蓝图或原型。
An object is a software bundle of related state and behavior. A class is a blueprint or prototype from which objects are created.
http://alfredjava.wordpress.com/2008/07/08/class-vs-object- vs-instance /
这篇关于注销扩展类的BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!