0

I have an class as below:

public class FYPSmsReceiverBroadcast extends BroadcastReceiver

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)

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);
    }

But this results in the following error in eclipse: 'FYPReceiverBroadcast cannot be resolved to a variable'

In the onReceive method of the class that extends Broadcast receiver I have a call to: FYPSpeakerActivity.speakSMSfrom();

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.

Can anyone advise?

GrumP
  • 1,183
  • 6
  • 19
  • 43
  • Do you have an object named **FYPReceiverBroadcast** which is an instance of the class **FYPSmsReceiverBroadcast**? If so, is it declared as a class variable? it should work. – Buffalo Aug 10 '12 at 13:18
  • No, I do not create an object. – GrumP Aug 10 '12 at 13:27

1 Answers1

3

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/

Caner
  • 57,267
  • 35
  • 174
  • 180
  • there is nothing in his question to suggest the fact that **FYPReceiverBroadcast** is a class, nor that he is following the naming conventions which would classify that name as a class name, not an object name. – Buffalo Aug 10 '12 at 13:22
  • 1
    You are right I cannot be 100% sure with information he posted, but he has classes named `FYPSmsReceiverBroadcast` & `FYPSpeakerActivity` so I guess `FYPReceiverBroadcast` is also a class. – Caner Aug 10 '12 at 13:25
  • Yes, it is a class. I do not create an object. So must I create an object of this class, before I can successfully unregister it? (my understanding of the above post) The application works as designed for the record, the force close error is the issue I'd like to resolve. – GrumP Aug 10 '12 at 13:29
  • That means he's trying to pass an object named 'FYPReceiverBroadcast' (which the compiler has nothing against) - it doesn't mean he has a class named 'FYPReceiverBroadcast' and he's trying to use it instead of an instance. I agree that this is NOT proper syntax, but he is asking why his code is not running, not how he can improve his syntax. Edit: nvm. – Buffalo Aug 10 '12 at 13:29
  • @Buffalo Actually he just said `FYPReceiverBroadcast` is a class. Btw 'xxx cannot be resolved to a variable' is a compile time error, not run time. – Caner Aug 10 '12 at 13:30
  • @Philleh how do you call `registerReceiver(...)`? Can you just post the whole source? – Caner Aug 10 '12 at 13:32
  • @Caner Sure, it's a mess though. All the logic for handling messages is in the onReceive. http://pastebin.com/ZMcRZwgQ – GrumP Aug 10 '12 at 13:41
  • Of course it's a compile time warning, because there's no variable by that name in scope. If it were, there wouldn't be a warning. Why would you say that? – Buffalo Aug 10 '12 at 13:47
  • @Philleh where do you call `registerReceiver`? – Caner Aug 10 '12 at 13:54
  • @Caner I don't. The onReceive method seems to just work. Bear with me, I need to take a closer look at the code - it's been a while. – GrumP Aug 10 '12 at 13:59
  • @Philleh I think you don't need to unregister the `FYPSmsReceiverBroadcast` becuase it extends `BroadcastReceiver`. And "a BroadcastReceiver object is only valid for the duration of the call". So it will be gone after `onReceive` finishes. See this for details: http://stackoverflow.com/questions/4957461/unregister-broadcast-receiver-android – Caner Aug 10 '12 at 14:04
  • @Philleh So I suspect the problem is maybe in `FYPSpeakerActivity` class. Can you post it's code? How about `FYPSmsReceiverBroadcast`? What is the difference between `FYPReceiverBroadcast` & `FYPSmsReceiverBroadcast`? – Caner Aug 10 '12 at 14:05
  • Apologies, typo in original post. Just an "FYPSmsReciverBroadcast" class. Here is the Speaker code: http://pastebin.com/1HPKERkn Apologies again for the mess, I wrote the original app almost 2 years ago. – GrumP Aug 10 '12 at 14:13
  • @Philleh ok, you call `requestLocationUpdates` but you never call `removeUpdates`. I guess it might be the problem. – Caner Aug 10 '12 at 14:20
  • On further inspection I'm convinced it's the listener for the text to speech functionality. When this line is hit inside FYPSmsReceiverBroadcast: FYPSpeakerActivity.speakSMSfrom(); I get the force close error during run-time - when the app should technically be closed. I'll look further into it - Thanks for your help! – GrumP Aug 13 '12 at 09:12