2013-06-26 48 views
-1

获取上线的错误:问题格式化变量的初始化在服务

String mdn = (settings.getString("mdn", mdn)); 

指出:局部变量MDN可能没有被初始化

的问题是,如果我捆绑额外= intent.getExtras();就在这条线的上方,我最终得到了“重复的局部变量extras”错误 - 如果我删除Bundle Extras的第一个实例 - 我会得到更多这些相同的错误。我只是需要一些帮助格式化它,所以它会执行没有任何错误。

任何输入,非常感谢。

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")) { 






        // 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); 



       Intent MDN = new Intent(this, MDNByteswapService.class); 

       StartActivity(MDN); 


        // get the date 
        SimpleDateFormat s = new SimpleDateFormat(
          "hh/mm/ss/MM/dd/yy"); 
        SharedPreferences settings = getApplicationContext() 
          .getSharedPreferences(Constants.PREFS_NAME, 0); 
        String tag = ";"; 

        String mdn = (settings.getString("mdn", mdn)); 
        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; 
     } 
     return startId; 
    } 

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

    } 

} 

源之后第一次编辑:

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")) { 

        // 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); 

        Intent MDN = new Intent(this, MDNByteswapService.class); 

        StartActivity(MDN); 

        // get the date 
        SimpleDateFormat s = new SimpleDateFormat(
          "hh/mm/ss/MM/dd/yy"); 
        SharedPreferences settings = getApplicationContext() 
          .getSharedPreferences(Constants.PREFS_NAME, 0); 
        String tag = ";"; 

        String mdn = (settings.getString("mdn", 
          "some_default_value_here")); 
        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; 
     } 
     return startId; 
    } 

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

    } 

} 

服务节省了MDN在SharedPreferences:

public class DataCountUtilities extends Service { 

    // swap the content of a string by switching place 
    // each pair of consecutive characters 
    // If string length is odd last character is left in place 
    public 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; 

      // 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(); 

     } 

     return String.valueOf(data); 

    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    // private void startActivity(Intent i) { 
    // TODO Auto-generated method stub 

} 

回答

0

您还没有初始化MDN呢。你不能创造一些东西并在创作中使用它。例如:

int x = 1+x;//how does the compiler know what x is? 

您不能在int x的初始化中创建use x,因为它不存在。请记住,Java是区分大小写的。你可能意味着:

String mdn = settings.getString("mdn", "default_string"); 

看一看的API:

http://developer.android.com/reference/android/content/SharedPreferences.html#getString%28java.lang.String,%20java.lang.String%29

0

你不能声明和一行像使用一个变量:

String mdn = (settings.getString("mdn", mdn)); 

getString中的第二个参数是默认值 - 如果没有带“mdn”键的条目,那就是您将从getString接收的值。

那么你或许应该替换符合:

String mdn = (settings.getString("mdn", "some_default_value_here")); 

或声明并初始化MDN之前:我不知道你在哪里,你提到“some_default_value_here” 我

String mdn = "some_default_value"; 
mdn = (settings.getString("mdn", mdn)); 
+0

m将它保存在另一个服务中使用: SharedPreferences设置= getApplicationContext() \t \t \t \t \t .getSharedPreferences(Constants.PREFS_NAME,0); \t \t \t Editor editor = settings.edit(); \t \t \t editor.putString(“mdn”,mdn); \t \t \t编辑。承诺(); 当我尝试检索它与some_default_value_here交换出来匹配 - 它不工作: String mdn =(settings.getString(“mdn”,mdn)); – user2525683

+0

'getString'需要返回一些东西。它带来的好处是,如果找不到该键的值,它可以返回一个默认值。如果你说你在阅读之前总是为该键写入一些值,那么你可以传递null(或任何字符串值),因为你会得到另一个值。是否有意义? – gunar

+0

是的 - 但我总是保存MDN - 这是电话号码 - 它不应该为空 P.S. 由于某种原因,我仍然没有从第二个服务中获取字节数的MDN - 它只是在短信中显示为“some_default_value_here”,我应该有一个字节码的MDN,而不是 – user2525683