本文介绍了信息= info.replace(QUOT; CN"," CN"," CN,QUOT; CO"");不能按预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有收集WiFi和蜂窝数据使用和将其作为通过SMS数据串的应用程序 - 但是我需要一点帮助修改它送入正确的格式字符串

的方式,现在看来:

  USI; 1; 3056090866; 06/16/58/06/24/13; CN25.48,WN86.957; CN34.931,WN16.656

我想它以下述方式发送的方式:(与第二CN和WN变为CO和WO)

  USI; 1; 3056090866; 06/16/58/06/24/13; CN25.48,WN86.957; CO34.931,WO16.656

这又如何实现呢?我已经尝试使用:

 信息= info.replace(CN,CN,CNCO);

 信息= info.replace(CN,CO);

但既不是给予期望的输出

P.S。

无论采用哪种方式,建议需要考虑到这串数据变化的数值 - 但字母CN和WN(蜂窝网络和无线网络)保持不变 - 我只是需要改变第二CN和WN中输出以WO和CO

完整的源代码:

 公共类DataCountService延伸服务{
    字符串文本=USR; 1;
    。字符串ERROR =常数preFS_NAME;
    私人定时器定时器=新的Timer();
    私人期长;
    私人长期delay_interval;    私人意图getIntent(){
        // TODO自动生成方法存根
        返回null;
    }    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志诠释startId){
        Log.d(Constants.TAG,记录服务启动);
        // super.onStartCommand(意向,旗帜,startId);        捆绑额外= intent.getExtras();
        如果(意向== NULL){
            //退出优雅的服务不是意图开始
            Log.d(Constants.TAG,错误:空意图);
        }其他{            如果(临时演员!= NULL){
                文字= extras.getString(Constants.DM_SMS_CONTENT);
                //检查启用或禁用的价值 - 如果设置为启用
                //检查启用或禁用的价值 - 如果设置为启用
                如果(extras.getString(Constants.DM_SMS_CONTENT)。载有(
                        // USR; 1)){
                    共享preferences设置= getApplicationContext()
                            .getShared preferences(常量preFS_NAME,0);
                    //获取WiFi和移动交通信息
                    双totalBytes =(双)TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes();
                    双mobileBytes = TrafficStats.getMobileRxBytes()
                            + TrafficStats.getMobileTxBytes();
                    totalBytes - = mobileBytes;
                    totalBytes / = 1000000;
                    mobileBytes / = 1000000;
                    NumberFormat的NF =新的DecimalFormat(####);
                    字符串状态=(settings.getString(状态,0));
                    字符串标记=;
                    字符串mobileStr = nf.format(mobileBytes);
                    字符串totalStr = nf.format(totalBytes);
                    //获取MDN                    TelephonyManager TM =(TelephonyManager)本
                            .getSystemService(Context.TELEPHONY_SERVICE);
                    //提取从TelephonyManager的电话号码
                    //实例
                    串MDN = tm.getLine1Number();
                    //投保MDN为10个字符
                    如果(mdn.length()小于10 || MDN == NULL)
                        MDN =0000000000;
                    //提取MDN的最后10位数字
                    如果(mdn.length()→10)
                        MDN = mdn.substring(mdn.length() - 10,mdn.length());
                    CHAR数据[] = mdn.toCharArray();
                    字符数字;
                    对于(INT指数= 0;指数 - LT; mdn.length() - (mdn.length())
                            %2;索引+ = 2){
                        数字=数据[指数]
                        数据[索引] =数据[索引+ 1];
                        数据[索引+ 1] =数字;                    }                    //获取日期
                    SimpleDateFormat的S =新的SimpleDateFormat(
                            HH / MM / SS / MM / DD / YY);
                    字符串DToDevice = s.format(新的Date());                    字符串信息=的String.format(USI%SCN%S,WN%S,标签+状态
                            +标签+ MDN +标签+ DToDevice +标签,mobileStr,
                            totalStr + settings.getString(LAST_MONTH,0));                    信息=USI+ info.replace(USI,);                    //通过手机短信或发送放大器交通信息;保存当前时间
                    SmsManager smsManager = SmsManager.getDefault();
                    如果(Config.DEVELOPMENT){
                        短字符串code = settings.getString(
                                常量。preFS_KEY_SHORT_ code,
                                Constants.DEFAULT_SHORT_ code);
                        smsManager.sendTextMessage(短code,空,信息,空,
                                空值);
                        //将状态设置为启用                        编辑编辑= settings.edit();
                        editor.putString(身份,1);
                        editor.commit();
                        editor.putLong(smstimestamp
                                System.currentTimeMillis的());
                        editor.commit();                    }其他{
                        SmsManager ackSMS = SmsManager.getDefault();
                        smsManager.sendTextMessage(
                                Constants.DEFAULT_SHORT_ code,空,信息,空,
                                空值);
                    }                    //检查启用或禁用的价值 - 如果设置为禁用
                }否则如果(extras.getString(Constants.DM_SMS_CONTENT)。载有(
                        // USR 0)){
                    //将状态设置为已禁用
                    共享preferences设置= getApplicationContext()
                            .getShared preferences(常量preFS_NAME,0);
                    编辑编辑= settings.edit();
                    editor.putString(身份,0);
                    editor.commit();
                    stopSelf();                    //检查启用或禁用的价值 - 如果设置为任何其他
                    //字符
                }            }
        }
        返回START_NOT_STICKY;
    }    私人意图意图(){
        // TODO自动生成方法存根
        返回null;
    }    @覆盖
    公共无效的onCreate(){        如果(Config.DEVELOPMENT){            周期= Constants.PERIOD;
            delay_interval = Constants.DELAY_INTERVAL;        }其他{
            捆绑额外= getIntent()getExtras()。
            周期= Constants.DEBUG_PERIOD;
            delay_interval = Constants.DEBUG_DELAY_INTERVAL;
        }
        startServiceTimer();
    }    私人无效startServiceTimer(){
        timer.schedule(新的TimerTask(){
            公共无效的run(){                共享preferences设置= getApplicationContext()
                        .getShared preferences(常量preFS_NAME,0);
                如果(settings.getString(身份,0)。等于(1)){                    //获取WiFi和移动交通信息
                    双totalBytes =(双)TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes();
                    双mobileBytes = TrafficStats.getMobileRxBytes()
                            + TrafficStats.getMobileTxBytes();
                    totalBytes - = mobileBytes;
                    totalBytes / = 1000000;
                    mobileBytes / = 1000000;
                    NumberFormat的NF =新的DecimalFormat(####);
                    字符串标记=;
                    字符串mobileStr = nf.format(mobileBytes);
                    字符串totalStr = nf.format(totalBytes);
                    字符串信息=的String.format(CO%S,WO%S标记,mobileStr,
                            totalStr);
                    //保存共享preferences网络及WiFi数据                    共享preferences cnwn = getApplicationContext()
                            .getShared preferences(常量preFS_NAME,0);
                    编辑编辑= cnwn.edit();
                    editor.putString(LAST_MONTH,信息);
                    editor.commit();                    //                    //发送短信(与WiFi的使用情况及最近一个月的数据使用)
                    //和
                    //保存当前的时间
                    字符串短信=;
                    短信+ =(CO+(TrafficStats.getMobileRxBytes()+ TrafficStats
                            .getMobileTxBytes())/ 1000000);
                    短信+ =(WO+(TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes() - (TrafficStats
                            .getMobileRxBytes()+ TrafficStats
                            .getMobileTxBytes()))/ 1000000);                    SmsManager smsManager = SmsManager.getDefault();
                    如果(Config.DEVELOPMENT){
                        短字符串code = settings.getString(
                                常量。preFS_KEY_SHORT_ code,
                                Constants.DEFAULT_SHORT_ code);
                        smsManager.sendTextMessage(短code,空,
                                短信+ cnwn.getString(LAST_MONTH,),空,
                                空值);
                        editor.putLong(smstimestamp
                                System.currentTimeMillis的());
                        editor.commit();
                    }其他{
                        SmsManager ackSMS = SmsManager.getDefault();
                        smsManager.sendTextMessage(
                                Constants.DEFAULT_SHORT_ code,空,
                                短信+ cnwn.getString(LAST_MONTH,),空,
                                空值);
                    }                }
            }
        },delay_interval,周期);    }    @覆盖
    公众的IBinder onBind(意向意图){        // TODO自动生成方法存根        返回null;    }    @覆盖
    公共布尔onUnbind(意向意图){        // TODO自动生成方法存根        返回super.onUnbind(意向);    }}

编辑回应VISHAL的回答是:

 私人无效startServiceTimer(){
    timer.schedule(新的TimerTask(){
        公共无效的run(){            共享preferences设置= getApplicationContext()
                    .getShared preferences(常量preFS_NAME,0);
            如果(settings.getString(身份,0)。等于(1)){                //获取WiFi和移动交通信息
                双totalBytes =(双)TrafficStats.getTotalRxBytes()
                        + TrafficStats.getTotalTxBytes();
                双mobileBytes = TrafficStats.getMobileRxBytes()
                        + TrafficStats.getMobileTxBytes();
                totalBytes - = mobileBytes;
                totalBytes / = 1000000;
                mobileBytes / = 1000000;
                NumberFormat的NF =新的DecimalFormat(####);
                字符串标记=;
                字符串mobileStr = nf.format(mobileBytes);
                字符串totalStr = nf.format(totalBytes);
                字符串信息=的String.format(CO%S,WO%S标记,mobileStr,
                        totalStr);
                //保存共享preferences网络及WiFi数据                共享preferences cnwn = getApplicationContext()
                        .getShared preferences(常量preFS_NAME,0);
                编辑编辑= cnwn.edit();
                editor.putString(LAST_MONTH,信息);
                editor.commit();                //                //发送短信(与WiFi的使用情况及最近一个月的数据使用)
                //和
                //保存当前的时间
                字符串短信=;
                短信+ =(CO+(TrafficStats.getMobileRxBytes()+ TrafficStats
                        .getMobileTxBytes())/ 1000000);
                短信+ =(WO+(TrafficStats.getTotalRxBytes()
                        + TrafficStats.getTotalTxBytes() - (TrafficStats
                        .getMobileRxBytes()+ TrafficStats
                        .getMobileTxBytes()))/ 1000000);
                StringBuilder的B =新的StringBuilder();
                b.replace(sms.lastIndexOf(CN) - 1,sms.lastIndexOf(CN)+ 2,CO);
                b.replace(sms.lastIndexOf(WN) - 1,sms.lastIndexOf(WN)+ 2,WO);
                短信= b.toString();                SmsManager smsManager = SmsManager.getDefault();
                如果(Config.DEVELOPMENT){
                    短字符串code = settings.getString(
                            常量。preFS_KEY_SHORT_ code,
                            Constants.DEFAULT_SHORT_ code);
                    smsManager.sendTextMessage(短code,空,
                            短信+ cnwn.getString(LAST_MONTH,),空,
                            空值);
                    editor.putLong(smstimestamp
                            System.currentTimeMillis的());
                    editor.commit();
                }其他{
                    SmsManager ackSMS = SmsManager.getDefault();
                    smsManager.sendTextMessage(
                            Constants.DEFAULT_SHORT_ code,空,
                            短信+ cnwn.getString(LAST_MONTH,),空,
                            空值);
                }            }


解决方案

试试这个code - >

 字符串str =USI; 1; 3056090866; 06/16/58/06/24/13; CN25.48,WN86.957; CN34.931,WN16.656 ;
StringBuilder的B =新的StringBuilder(STR);
b.replace(str.lastIndexOf(CN) - 1,str.lastIndexOf(CN)+ 2,CO);
b.replace(str.lastIndexOf(WN) - 1,str.lastIndexOf(WN)+ 2,WO);
海峡= b.toString();

这将工作..

I have an application which gathers wifi and cellular data usage and sends it as a string of data via SMS - however I need a bit of help modifying the string it sends into the correct format

The way it appears now:

USI;1;3056090866;06/16/58/06/24/13;CN25.48,WN86.957;CN34.931,WN16.656

The way I would like it to send in the following manner: (with the second CN and WN changed to CO and WO)

USI;1;3056090866;06/16/58/06/24/13;CN25.48,WN86.957;CO34.931,WO16.656

How can this be accomplished? I've attempted to use:

info = info.replace("CN", "CN", "CN, "CO"");

and

info = info.replace("CN", "CO"");

but neither is giving the expected output

P.S.

Whatever method is suggested needs to take into account the numeric values in this string of data change - but the letters CN and WN (Cellular Network and Wireless network) stay the same - I simply need to change the 2nd CN and WN in the output to WO and CO

FULL SOURCE:

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("#.###");
                    String status = (settings.getString("status", "0"));
                    String tag = ";";
                    String mobileStr = nf.format(mobileBytes);
                    String totalStr = nf.format(totalBytes);
                    // get the MDN

                    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;

                    }

                    // get the date
                    SimpleDateFormat s = new SimpleDateFormat(
                            "hh/mm/ss/MM/dd/yy");
                    String DToDevice = s.format(new Date());

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

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

    }

}

EDIT IN RESPONSE TO VISHAL'S RESPONSE:

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


                StringBuilder b = new StringBuilder();
                b.replace(sms.lastIndexOf("CN") - 1, sms.lastIndexOf("CN") + 2, "CO" );
                b.replace(sms.lastIndexOf("WN") - 1, sms.lastIndexOf("WN") + 2, "WO" );
                sms = b.toString();

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

            }
解决方案

Try this code->

String str = "USI;1;3056090866;06/16/58/06/24/13;CN25.48,WN86.957;CN34.931,WN16.656";
StringBuilder b = new StringBuilder(str);
b.replace(str.lastIndexOf("CN") - 1, str.lastIndexOf("CN") + 2, "CO" );
b.replace(str.lastIndexOf("WN") - 1, str.lastIndexOf("WN") + 2, "WO" );
str = b.toString();

It will work..

这篇关于信息= info.replace(QUOT; CN&QUOT;,&QUOT; CN&QUOT;,&QUOT; CN,QUOT; CO&QUOT;&QUOT;);不能按预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:10