0

我正在尝试在服务中实现电话状态侦听器,但出现错误:

Syntax error on token "extends", throws expected

但是当我更改为时extendsthrows我收到 3 个新错误,说明:

EndCallListener cannot be resolved to a type 
EndCallListener cannot be resolved to a type
No exception of type PhoneStateListener can be thrown; an exception type must be a subclass of Throwable

有什么建议么?

阿曼尼·斯旺

public void onCreate() extends PhoneStateListener {
  EndCallListener callListener = new EndCallListener();
  TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
   mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
 }

 public void onCallStateChanged(int state, String incomingNumber) {
  if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
    // set number of calls to 1 in SharedPreferences                    
   SharedPreferences pref = getApplicationContext()
     .getSharedPreferences("DataCountService", 0);
   Editor editor = pref.edit();
    editor.putString("calls", "1");
   editor.commit();
  }
4

1 回答 1

0

如果 onCreate 实际上是一个方法,如返回类型“void”和名称后的括号所示,则不能在那里使用 extends。只有类可以继承其他类。

public class ClassName extends ParentClassName { //class code }

在这里,您可以找到有关继承的oracles 教程。

于 2013-06-19T17:07:27.703 回答