views:

30

answers:

1

The situation is that I have my listener constructed using an anonymous inner class, as is typical, but the way of deregistering a [PhoneStateListener][1] in Android requires me to pass the listener object to the this same function that I used to register it, but use the LISTEN_NONE flag. The problem is that I can't do this with an anonymous inner class because it's, well, anonymous. Do I have to instantiate my class with a name to be able to de-register it, or can I just ignore this problem and my listener will disappear by itself when my service terminates?

[1]: http://developer.android.com/reference/android/telephony/TelephonyManager.html#listen(android.telephony.PhoneStateListener, int)

+1  A: 

You can assign your anonymous class to a variable, and thus pass it in multiple places:

PhoneStateListener listener = new PhoneStateListener() {
    // class definition;
  }
Mayra
Perfect! I'm a bit new to anonymous classes so I didn't realise this was possible. Cheers.
Sam Svenbjorgchristiensensen