getSystemService error

Put your problem here if it does not fit any of the other categories.

getSystemService error

Postby ablin » Sun Jan 24, 2010 3:51 pm

button3.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
exPhoneCallListener myPhoneCallListener = new exPhoneCallListener();
TelephonyManager tm = (TelephonyManager)
this.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(myPhoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
}
});

at getSystemService is error

what can i do?


I want to use a button to openclose a phoneListener.
ablin
Freshman
Freshman
 
Posts: 4
Joined: Mon Jan 11, 2010 8:59 am

Top

Postby grzegorz.zajac » Sun Jan 24, 2010 4:28 pm

Hello,

In your code, inside the onClick() method, this is not a reference to the Context object, rather it is reference to the View.OnClickListener().

As getSystemService() is Context object method, your code should be:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onClick(View v) {
  2.    exPhoneCallListener myPhoneCallListener = new exPhoneCallListener();
  3.    TelephonyManager tm = (TelephonyManager) v.getContext().getSystemService(Context.TELEPHONY_SERVICE);
  4.    tm.listen(myPhoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
  5. }
  6.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Regards,
Grzesiek
grzegorz.zajac
Developer
Developer
 
Posts: 39
Joined: Wed Feb 18, 2009 8:37 pm
Location: Krakow/Poland

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 38 guests