Syntax error on tokens, ConstructorHeaderName expected instead & Syntax error on token “(”, < expected

只愿长相守 提交于 2019-12-12 20:35:19

问题


I'm getting two errors stating Syntax error on tokens, ConstructorHeaderName expected instead & Syntax error on token "(", < expected

on the line:

mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

...any suggestions?

public class DataCountService extends Service {
    String text = "USR;1";
    String ERROR = Constants.PREFS_NAME;
    private Timer timer = new Timer();
    private long period;
    private long delay_interval;




    EndCallListener callListener = new EndCallListener();
    TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
    mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

    private class EndCallListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if(TelephonyManager.CALL_STATE_RINGING == state) {
              //  Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
            }
            if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
                //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
              //  Log.i(LOG_TAG, "OFFHOOK");
            }
            if(TelephonyManager.CALL_STATE_IDLE == state) {
                //when this state occurs, and your flag is set, restart your app
              //  Log.i(LOG_TAG, "IDLE");
            }
        }
    }

回答1:


The line

mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

is not included in any method. You should put this in the constructor(which seems like what you want) or a regular method.




回答2:


mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

Should be inside a method, not directly inside class. Refer this question for example implementation.



来源:https://stackoverflow.com/questions/17352905/syntax-error-on-tokens-constructorheadername-expected-instead-syntax-error-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!