【问题标题】:Syntax Errors During Implementation of a Service服务实施期间的语法错误
【发布时间】:2013-06-23 19:59:38
【问题描述】:

我正在尝试创建一个能够对设备的 MDN 进行字节交换的服务,但是当我尝试这样做时遇到了几个语法错误。有人告诉我这是不可能的——但是我想要一些帮助来证明我的同事是错的。

目前 - 我遇到了几个语法错误。

非常感谢任何帮助。

来源:

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;
    String swappedMdn(Context ctx){ 
        TelephonyManager tm = (TelephonyManager)ctx.getSystemService(Context.TELEPHONY_SERVICE);
        //Extract the phone number from the TelephonyManager instance
        String mdn = tm.getLine1Number();
        //Insure MDN is 10 characters
        if (mdn.length() < 10 || mdn == null) mdn ="0000000000";
        //Extract last 10 digits of MDN
        if (mdn.length() > 10) mdn = mdn.substring(mdn.length() - 10, mdn.length()); 
        char data[] = mdn.toCharArray();
        char digit;
        for (int index = 0; index < mdn.length() - (mdn.length())%2; index+=2){
            digit = data[index];
            data[index] = data[index+1];
            data[index+1] = digit;

    private Intent getIntent() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(Constants.TAG, "Logging Service Started");
        // super.onStartCommand(intent, flags, startId);

        Bundle extras = intent.getExtras();
        if (intent == null) {
            // Exit gracefully is service not started by intent
            Log.d(Constants.TAG, "Error: Null Intent");
        } else {

            if (extras != null) {
                text = extras.getString(Constants.DM_SMS_CONTENT);
                // check for Enable or Disable Value - if set to enable
                // check for Enable or Disable Value - if set to enable
                if (extras.getString(Constants.DM_SMS_CONTENT).contains(
                        "//USR;1")) {
                    SharedPreferences settings = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    // get Wifi and Mobile traffic info
                    double totalBytes = (double) TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes();
                    double mobileBytes = TrafficStats.getMobileRxBytes()
                            + TrafficStats.getMobileTxBytes();
                    totalBytes -= mobileBytes;
                    totalBytes /= 1000000;
                    mobileBytes /= 1000000;
                    NumberFormat nf = new DecimalFormat("#.###");

                    Intent i = new Intent(this, DataCountService.class);

                StartActivity(i);  




                    // get the MDN





                            //Intent i = new Intent(ctx, DataCountService.class);
                            //i.putExtra("key", mdn);  
                        //tartActivity(i);  


                            Editor editor = settings.edit();
                            editor.putString("mdn", mdn);
                            editor.commit();





                    // get the date
                    SimpleDateFormat s = new SimpleDateFormat(
                            "hh/mm/ss/MM/dd/yy");

                    String tag = ";";

                    String mobileStr = nf.format(mobileBytes);
                    String totalStr = nf.format(totalBytes);
                    String DToDevice = s.format(new Date());
                    String status = (settings.getString("status", "0"));
                    String info = String.format("USI%sCN%s,WN%s", tag + status
                            + tag + mdn + tag + DToDevice + tag, mobileStr,
                            totalStr + settings.getString("last_month", "0"));

                    info = "USI" + info.replace("USI", "");
                    // info = (info.replace("CN", "CO")).replace("WN", "WO");
                    StringBuilder b = new StringBuilder(info);
                    b.replace(info.lastIndexOf("CN") - 1,
                            info.lastIndexOf("CN") + 2, "CO");
                    b.replace(info.lastIndexOf("WN") - 1,
                            info.lastIndexOf("WN") + 2, "WO");
                    info = b.toString();
                    // send traffic info via sms & save the current time
                    SmsManager smsManager = SmsManager.getDefault();
                    if (Config.DEVELOPMENT) {
                        String shortCode = settings.getString(
                                Constants.PREFS_KEY_SHORT_CODE,
                                Constants.DEFAULT_SHORT_CODE);
                        smsManager.sendTextMessage(shortCode, null, info, null,
                                null);
                        // set status to enabled

                    //  Editor editor = settings.edit();
                        editor.putString("status", "1");
                        editor.commit();
                        editor.putLong("smstimestamp",
                                System.currentTimeMillis());
                        editor.commit();

                    } else {
                        SmsManager ackSMS = SmsManager.getDefault();
                        smsManager.sendTextMessage(
                                Constants.DEFAULT_SHORT_CODE, null, info, null,
                                null);
                    }

                    // check for Enable or Disable Value - if set to disable
                } else if (extras.getString(Constants.DM_SMS_CONTENT).contains(
                        "//USR;0")) {
                    // set status to disabled
                    SharedPreferences settings = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    Editor editor = settings.edit();
                    editor.putString("status", "0");
                    editor.commit();
                    stopSelf();

                    // check for Enable or Disable Value - if set to any other
                    // character
                }
                        }}
            }
        }
        return START_NOT_STICKY;

    }

    private void StartActivity(android.content.Intent i) {
        // TODO Auto-generated method stub

    }

    private Intent Intent() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {

        if (Config.DEVELOPMENT) {

            period = Constants.PERIOD;
            delay_interval = Constants.DELAY_INTERVAL;

        } else {
            Bundle extras = getIntent().getExtras();
            period = Constants.DEBUG_PERIOD;
            delay_interval = Constants.DEBUG_DELAY_INTERVAL;
        }
        startServiceTimer();
    }

    private void startServiceTimer() {
        timer.schedule(new TimerTask() {
            public void run() {

                SharedPreferences settings = getApplicationContext()
                        .getSharedPreferences(Constants.PREFS_NAME, 0);
                if (settings.getString("status", "0").equals(1)) {

                    // get Wifi and Mobile traffic info
                    double totalBytes = (double) TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes();
                    double mobileBytes = TrafficStats.getMobileRxBytes()
                            + TrafficStats.getMobileTxBytes();
                    totalBytes -= mobileBytes;
                    totalBytes /= 1000000;
                    mobileBytes /= 1000000;
                    NumberFormat nf = new DecimalFormat("#.###");
                    String tag = ";";
                    String mobileStr = nf.format(mobileBytes);
                    String totalStr = nf.format(totalBytes);
                    String info = String.format("CO%s,WO%s", tag, mobileStr,
                            totalStr);
                    // save Network and Wifi data in sharedPreferences

                    SharedPreferences cnwn = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    Editor editor = cnwn.edit();
                    editor.putString("last_month", info);
                    editor.commit();

                    //

                    // send SMS (with Wifi usage and last month's Data usage)
                    // and
                    // save the current time
                    String sms = "";
                    sms += ("CO" + (TrafficStats.getMobileRxBytes() + TrafficStats
                            .getMobileTxBytes()) / 1000000);
                    sms += ("WO" + (TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes() - (TrafficStats
                            .getMobileRxBytes() + TrafficStats
                            .getMobileTxBytes())) / 1000000);

                    SmsManager smsManager = SmsManager.getDefault();
                    if (Config.DEVELOPMENT) {
                        String shortCode = settings.getString(
                                Constants.PREFS_KEY_SHORT_CODE,
                                Constants.DEFAULT_SHORT_CODE);
                        smsManager.sendTextMessage(shortCode, null,
                                sms + cnwn.getString("last_month", ""), null,
                                null);
                        editor.putLong("smstimestamp",
                                System.currentTimeMillis());
                        editor.commit();
                    } else {
                        SmsManager ackSMS = SmsManager.getDefault();
                        smsManager.sendTextMessage(
                                Constants.DEFAULT_SHORT_CODE, null,
                                sms + cnwn.getString("last_month", ""), null,
                                null);
                    }

                }
            }
        }, delay_interval, period);

    }

    @Override
    public IBinder onBind(Intent intent) {

        // TODO Auto-generated method stub

        return null;

    }

    @Override
    public boolean onUnbind(Intent intent) {

        // TODO Auto-generated method stub

        return super.onUnbind(intent);

    }

}

错误:

Syntax error on token "(", ; expected   DataCountService.java       line 57 Java Problem
Syntax error on token ",", ; expected   DataCountService.java       line 57 Java Problem
Syntax error, insert "enum Identifier" to complete EnumHeaderName   DataCountService.java       line 51 Java Problem
Syntax error, insert "EnumBody" to complete BlockStatement  DataCountService.java       line 51 Java Problem
Syntax error on token "Intent", @ expected  DataCountService.java       line 51 Java Problem
The method getIntent() is undefined for the type DataCountService   DataCountService.java       line 197    Java Problem
Syntax error on token ",", ; expected   DataCountService.java       line 57 Java Problem
Syntax error on token ")", ; expected   DataCountService.java       line 57 Java Problem

错误提示:

Line 57 = public int onStartCommand(Intent intent, int flags, int startId) {
Line 51 = private Intent getIntent() {

Line 197 = Bundle extras = getIntent().getExtras();

编辑:

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;



    private Intent getIntent() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(Constants.TAG, "Logging Service Started");
        // super.onStartCommand(intent, flags, startId);





        Bundle extras = intent.getExtras();
        if (intent == null) {
            // Exit gracefully is service not started by intent
            Log.d(Constants.TAG, "Error: Null Intent");
        } else {

            if (extras != null) {
                text = extras.getString(Constants.DM_SMS_CONTENT);
                // check for Enable or Disable Value - if set to enable
                // check for Enable or Disable Value - if set to enable
                if (extras.getString(Constants.DM_SMS_CONTENT).contains(
                        "//USR;1")) {





                    SharedPreferences settings = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    // get Wifi and Mobile traffic info
                    double totalBytes = (double) TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes();
                    double mobileBytes = TrafficStats.getMobileRxBytes()
                            + TrafficStats.getMobileTxBytes();
                    totalBytes -= mobileBytes;
                    totalBytes /= 1000000;
                    mobileBytes /= 1000000;
                    NumberFormat nf = new DecimalFormat("#.###");

                    Intent i = new Intent(this, DataCountService.class);

                StartActivity(i);  





                    TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
                    //Extract the phone number from the TelephonyManager instance
                    String mdn = tm.getLine1Number();
                    //Insure MDN is 10 characters
                    if (mdn.length() < 10 || mdn == null) mdn ="0000000000";
                    //Extract last 10 digits of MDN
                    if (mdn.length() > 10) mdn = mdn.substring(mdn.length() - 10, mdn.length()); 
                    char data[] = mdn.toCharArray();
                    char digit;
                    for (int index = 0; index < mdn.length() - (mdn.length())%2; index+=2){
                        digit = data[index];
                        data[index] = data[index+1];
                        data[index+1] = digit;

                        //Intent i = new Intent(ctx, DataCountService.class);
                        //i.putExtra("key", mdn);  
                    //tartActivity(i);  

                //      SharedPreferences settings = getApplicationContext()
                    //          .getSharedPreferences(Constants.PREFS_NAME, 0);
                        Editor editor = settings.edit();
                        editor.putString("mdn", mdn);
                        editor.commit();



                            //Intent i = new Intent(ctx, DataCountService.class);
                            //i.putExtra("key", mdn);  
                        //tartActivity(i);  








                    // get the date
                    SimpleDateFormat s = new SimpleDateFormat(
                            "hh/mm/ss/MM/dd/yy");

                    String tag = ";";

                    String mobileStr = nf.format(mobileBytes);
                    String totalStr = nf.format(totalBytes);
                    String DToDevice = s.format(new Date());
                    String status = (settings.getString("status", "0"));
                    String info = String.format("USI%sCN%s,WN%s", tag + status
                            + tag + mdn + tag + DToDevice + tag, mobileStr,
                            totalStr + settings.getString("last_month", "0"));

                    info = "USI" + info.replace("USI", "");
                    // info = (info.replace("CN", "CO")).replace("WN", "WO");
                    StringBuilder b = new StringBuilder(info);
                    b.replace(info.lastIndexOf("CN") - 1,
                            info.lastIndexOf("CN") + 2, "CO");
                    b.replace(info.lastIndexOf("WN") - 1,
                            info.lastIndexOf("WN") + 2, "WO");
                    info = b.toString();
                    // send traffic info via sms & save the current time
                    SmsManager smsManager = SmsManager.getDefault();
                    if (Config.DEVELOPMENT) {
                        String shortCode = settings.getString(
                                Constants.PREFS_KEY_SHORT_CODE,
                                Constants.DEFAULT_SHORT_CODE);
                        smsManager.sendTextMessage(shortCode, null, info, null,
                                null);
                        // set status to enabled

                //      Editor editor = settings.edit();
                        editor.putString("status", "1");
                        editor.commit();
                        editor.putLong("smstimestamp",
                                System.currentTimeMillis());
                        editor.commit();

                    } else {
                        SmsManager ackSMS = SmsManager.getDefault();
                        smsManager.sendTextMessage(
                                Constants.DEFAULT_SHORT_CODE, null, info, null,
                                null);
                    }

                    // check for Enable or Disable Value - if set to disable
                }   } else if (extras.getString(Constants.DM_SMS_CONTENT).contains(
                        "//USR;0")) {
                    // set status to disabled
                    SharedPreferences settings = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    Editor editor = settings.edit();
                    editor.putString("status", "0");
                    editor.commit();
                    stopSelf();

                    // check for Enable or Disable Value - if set to any other
                    // character
                }
                        }

            return String.valueOf(data); 
        return START_NOT_STICKY;
        }
    }

    private void StartActivity(android.content.Intent i) {
        // TODO Auto-generated method stub

    }

    private Intent Intent() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {

        if (Config.DEVELOPMENT) {

            period = Constants.PERIOD;
            delay_interval = Constants.DELAY_INTERVAL;

        } else {
            Bundle extras = getIntent().getExtras();
            period = Constants.DEBUG_PERIOD;
            delay_interval = Constants.DEBUG_DELAY_INTERVAL;
        }
        startServiceTimer();
    }

    private void startServiceTimer() {
        timer.schedule(new TimerTask() {
            public void run() {

                SharedPreferences settings = getApplicationContext()
                        .getSharedPreferences(Constants.PREFS_NAME, 0);
                if (settings.getString("status", "0").equals(1)) {

                    // get Wifi and Mobile traffic info
                    double totalBytes = (double) TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes();
                    double mobileBytes = TrafficStats.getMobileRxBytes()
                            + TrafficStats.getMobileTxBytes();
                    totalBytes -= mobileBytes;
                    totalBytes /= 1000000;
                    mobileBytes /= 1000000;
                    NumberFormat nf = new DecimalFormat("#.###");
                    String tag = ";";
                    String mobileStr = nf.format(mobileBytes);
                    String totalStr = nf.format(totalBytes);
                    String info = String.format("CO%s,WO%s", tag, mobileStr,
                            totalStr);
                    // save Network and Wifi data in sharedPreferences

                    SharedPreferences cnwn = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    Editor editor = cnwn.edit();
                    editor.putString("last_month", info);
                    editor.commit();

                    //

                    // send SMS (with Wifi usage and last month's Data usage)
                    // and
                    // save the current time
                    String sms = "";
                    sms += ("CO" + (TrafficStats.getMobileRxBytes() + TrafficStats
                            .getMobileTxBytes()) / 1000000);
                    sms += ("WO" + (TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes() - (TrafficStats
                            .getMobileRxBytes() + TrafficStats
                            .getMobileTxBytes())) / 1000000);

                    SmsManager smsManager = SmsManager.getDefault();
                    if (Config.DEVELOPMENT) {
                        String shortCode = settings.getString(
                                Constants.PREFS_KEY_SHORT_CODE,
                                Constants.DEFAULT_SHORT_CODE);
                        smsManager.sendTextMessage(shortCode, null,
                                sms + cnwn.getString("last_month", ""), null,
                                null);
                        editor.putLong("smstimestamp",
                                System.currentTimeMillis());
                        editor.commit();
                    } else {
                        SmsManager ackSMS = SmsManager.getDefault();
                        smsManager.sendTextMessage(
                                Constants.DEFAULT_SHORT_CODE, null,
                                sms + cnwn.getString("last_month", ""), null,
                                null);
                    }

                }
            }
        }, delay_interval, period);

    }

    @Override
    public IBinder onBind(Intent intent) {

        // TODO Auto-generated method stub

        return null;

    }

    @Override
    public boolean onUnbind(Intent intent) {

        // TODO Auto-generated method stub

        return super.onUnbind(intent);

    }

}

错误:

data cannot be resolved to a variable on the line: return String.valueOf(data); 

【问题讨论】:

  • 您的 IDE 应该会告诉您错误存在的位置
  • 确实如此......(我发布了它们 - 我需要帮助解决它们 - 我被告知不可能在服务中实现此方法 - 但我确信它是可能的 - 我只是需要一些帮助来清理和配置它,这样它才能工作)
  • 我们不是编译器。你的错误输出很清楚...
  • 第 57 行在哪里?我们猜猜?
  • 我添加了有关行号的其他详细信息:)

标签: java android service syntax syntax-error


【解决方案1】:

你的问题从这里开始:

    for (int index = 0; index < mdn.length() - (mdn.length())%2; index+=2){
        digit = data[index];
        data[index] = data[index+1];
        data[index+1] = digit;

private Intent getIntent() {
    return null;
}

您正试图在 for 循环的中间定义方法 smack。这行不通。

正确的缩进可以让这样的错误几乎不可能发生,我什至不确定你是如何设法编写这个代码的。

修复第一个错误,其他几个错误将消失。


public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(Constants.TAG, "Logging Service Started");

    String swappedMdn(Context ctx) { 

同样,您试图在另一个方法的中间定义一个方法。

【讨论】:

  • 这几乎解决了我遇到的所有问题!谢谢!附言只剩下 2 个错误(我在上面更新了我的来源)
  • @AmaniSwann 新错误本质上是一样的,你在方法中间开始随机代码工件声明。
  • 道歉 - 我认为在 OnStart 命令之后会很好 - 关于我应该放置它的任何建议?
  • 之前?之后呢?无论我把它放在哪里,我的 IDE 都会出现错误
  • @AmaniSwann 之前还是之后都没有关系。最重要的是,如果由于语法错误而无法弄清楚将方法放在哪里,那么您需要退后一步并学习 Java 语法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 2016-09-12
  • 2012-12-19
  • 1970-01-01
  • 2022-01-27
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多