java 令牌上的语法错误,应为 ConstructorHeaderName 和令牌上的语法错误“(”,< 预期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17352905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:46:09  来源:igfitidea点击:

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

javaandroidsyntaxtelephonymanager

提问by user2528574

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

我收到两个错误,说明令牌上的语法错误,ConstructorHeaderName 预期而不是令牌“(”,< 预期的语法错误

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

回答by zw324

The line

线

mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

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.

不包含在任何方法中。你应该把它放在构造函数(这看起来像你想要的)或常规方法中。

回答by kosa

mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

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

应该在方法内部,而不是直接在类内部。请参阅此问题以获取示例实现。