本文介绍了不可达code - 返回startId;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一些功能添加到我的源$ C ​​$ C登录之前,我执行我的code的剩余部分(其收集WiFi和细胞的总量至少一个电话已取得的事实数据的使用,并通过手机短信发送的话)然而,当我增加了code保存至少一个电话已取得我结束了一个错误,说明事实无法到达code - 返回startId;而我不知道是什么原因造成的问题。

在此先感谢!

源获取错误:无法到达code - 返回startId;

 公共类DataCountService延伸服务{
字符串文本=USR; 1;
。字符串ERROR =常数preFS_NAME;
私人定时器定时器=新的Timer();
私人期长;
私人长期delay_interval;公共静态最后弦乐swappedMdn(上下文CTX){
    TelephonyManager TM =(TelephonyManager)CTX
            .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] =数字;        意图I =新意图(CTX,DataCountService.class);        共享preferences设置= ctx.getShared preferences(
                常量preFS_NAME,0)。
        编辑编辑= settings.edit();
        editor.putString(0,新的字符串(数据));
        editor.commit();    }    返回将String.valueOf(数据);}私人意图getIntent(){
    // TODO自动生成方法存根
    返回null;
}公众诠释onStartCommand(意向意图,诠释旗帜,诠释startId,INT的状态,
        字符串incomingNumber){
    Log.d(Constants.TAG,记录服务启动);    捆绑额外= intent.getExtras();    如果(意向== NULL){        //优雅地退出,如果服务没有启动的意图
        Log.d(Constants.TAG,错误:空意图);
    }其他{        如果(临时演员!= NULL){            如果(TelephonyManager.CALL_STATE_OFFHOOK ==州){
                //等待电话去摘机(可能设置一个布尔值
                //标志),所以你
                //知道您的应用程序发起呼叫。
                // Log.i(LOG_TAG,摘机);
                共享preferences设置= getApplicationContext()
                        .getShared preferences(常量preFS_NAME,0);
                编辑编辑= settings.edit();
                editor.putString(callMade,1);
                editor.commit();            }其他{                //共享preferences设置= getApplicationContext()
                // .getShared preferences(常量preFS_NAME,0);
                //如果(settings.getString(callsMade,1)。等于(1)){                字符串newMdn = swappedMdn(本);                文字= extras.getString(Constants.DM_SMS_CONTENT);                //检查启用或禁用的价值 - 如果设置为启用
                如果(extras.getString(Constants.DM_SMS_CONTENT)。载有(
                        // USR; 1)){                    //获取WiFi和移动交通信息
                    双totalBytes =(双)TrafficStats
                            .getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes();
                    双mobileBytes = TrafficStats.getMobileRxBytes()
                            + TrafficStats.getMobileTxBytes();
                    totalBytes - = mobileBytes;
                    totalBytes / = 1000000;
                    mobileBytes / = 1000000;
                    NumberFormat的NF =新的DecimalFormat(####);                    //获取日期
                    SimpleDateFormat的S =新的SimpleDateFormat(
                            HH / MM / SS / MM / DD / YY);                    字符串标记=;
                    字符串mobileStr = nf.format(mobileBytes);
                    字符串totalStr = nf.format(totalBytes);
                    字符串DToDevice = s.format(新的Date());
                    共享preferences设置= getApplicationContext()
                            .getShared preferences(常量preFS_NAME,0);
                    字符串状态=(settings.getString(状态,0));
                    字符串信息=字符串
                            .format(USI%SCN%S,WN%S,标签+状态+标签
                                    + settings.getString(0,newMdn)+标签
                                    + DToDevice +标签,mobileStr,totalStr
                                    + settings.getString(LAST_MONTH,0));
                    信息=USI+ info.replace(USI,);                    //信息=(info.replace(CN,CO))。替换(WN
                    //WO);
                    StringBuilder的B =新的StringBuilder(信息);
                    b.replace(info.lastIndexOf(CN) - 1,
                            info.lastIndexOf(CN)+2CO);
                    b.replace(info.lastIndexOf(WN) - 1,
                            info.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,空,信息,
                                NULL,NULL);                        //将状态设置为启用
                        编辑编辑= settings.edit();
                        editor.putString(身份,1);
                        editor.commit();
                        editor.putLong(smstimestamp
                                System.currentTimeMillis的());
                        editor.commit();                    }其他{
                        SmsManager ackSMS = SmsManager.getDefault();
                        smsManager.sendTextMessage(
                                Constants.DEFAULT_SHORT_ code,空,信息,
                                NULL,NULL);
                    }
                }                //检查启用或禁用的价值 - 如果设置为禁用
            }
        }否则如果(extras.getString(Constants.DM_SMS_CONTENT)。载有(
                // USR 0)){            //将状态设置为已禁用
            共享preferences设置= getApplicationContext()
                    .getShared preferences(常量preFS_NAME,0);
            编辑编辑= settings.edit();
            editor.putString(身份,0);
            editor.commit();
            stopSelf();        }
    }
    返回START_STICKY;    返回startId;
}私人无效StartActivity(android.content.Intent我){
    // TODO自动生成方法存根}私人意图意图(){
    // 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(意向);}

}

SOURCE新的code /错误之前(正常工作):

 公共类DataCountService延伸服务{
        字符串文本=USR; 1;
        。字符串ERROR =常数preFS_NAME;
        私人定时器定时器=新的Timer();
        私人期长;
        私人长期delay_interval;    公共静态最后弦乐swappedMdn(上下文CTX){
        TelephonyManager TM =(TelephonyManager)CTX
                .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] =数字;             意图I =新意图(CTX,DataCountService.class);
                共享preferences设置= ctx.getShared preferences(常量preFS_NAME,0);
                编辑编辑= settings.edit();
                editor.putString(0,新的字符串(数据));
                editor.commit();        }        返回将String.valueOf(数据);    }
    私人意图getIntent(){
        // TODO自动生成方法存根
        返回null;
    }    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志诠释startId){
        Log.d(Constants.TAG,记录服务启动);
        捆绑额外= intent.getExtras();        如果(意向== NULL){            //优雅地退出,如果服务没有启动的意图
            Log.d(Constants.TAG,错误:空意图);
        }其他{            如果(临时演员!= NULL){                字符串newMdn = swappedMdn(本);                文字= extras.getString(Constants.DM_SMS_CONTENT);                //检查启用或禁用的价值 - 如果设置为启用
                如果(extras.getString(Constants.DM_SMS_CONTENT)。载有(
                        // USR; 1)){                    //获取WiFi和移动交通信息
                    双totalBytes =(双)TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes();
                    双mobileBytes = TrafficStats.getMobileRxBytes()
                            + TrafficStats.getMobileTxBytes();
                    totalBytes - = mobileBytes;
                    totalBytes / = 1000000;
                    mobileBytes / = 1000000;
                    NumberFormat的NF =新的DecimalFormat(####);
                    //获取日期
                    SimpleDateFormat的S =新的SimpleDateFormat(
                            HH / MM / SS / MM / DD / YY);
                    共享preferences设置= getApplicationContext()
                            .getShared preferences(常量preFS_NAME,0);
                    字符串标记=;
                    //字符串MDN = extras.getString(DataCountUtilities.swappedMdn(本));
                    字符串mobileStr = nf.format(mobileBytes);
                    字符串totalStr = nf.format(totalBytes);
                    字符串DToDevice = s.format(新的Date());
                    字符串状态=(settings.getString(状态,0));
                    字符串信息=的String.format(USI%SCN%S,WN%S,标签+状态
                            +标签+ settings.getString(0,newMdn)+标签+ DToDevice +标签,mobileStr,
                            totalStr + settings.getString(LAST_MONTH,0));
                    信息=USI+ info.replace(USI,);                    //信息=(info.replace(CN,CO))代替(WN,沃)。
                    StringBuilder的B =新的StringBuilder(信息);
                    b.replace(info.lastIndexOf(CN) - 1,
                            info.lastIndexOf(CN)+2CO);
                    b.replace(info.lastIndexOf(WN) - 1,
                            info.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,空,信息,空,
                                空值);                        //将状态设置为启用
                        编辑编辑= 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_STICKY;
        }
        返回startId;
    }    私人无效StartActivity(android.content.Intent我){
        // TODO自动生成方法存根    }    私人意图意图(){
        // 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(意向);    }    }


解决方案

只要保持有一个收益

 返回START_STICKY;返回startId;

I'm attempting to add some functionality to my source code to log the fact at least one phone call has been made before I execute the rest of my code (which collects total amount of wifi and cell data usage and sends it via sms) however when I added the code to save the fact at least one phone call has been made I ended up with an error stating "Unreachable Code - return startId;" and I'm not sure what is causing the issue.

Thanks in advance!

SOURCE GETTING ERROR: Unreachable Code - return startId;

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;

public static final 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);

        SharedPreferences settings = ctx.getSharedPreferences(
                Constants.PREFS_NAME, 0);
        Editor editor = settings.edit();
        editor.putString("0", new String(data));
        editor.commit();

    }

    return String.valueOf(data);

}

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

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

    Bundle extras = intent.getExtras();

    if (intent == null) {

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

        if (extras != null) {

            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");
                SharedPreferences settings = getApplicationContext()
                        .getSharedPreferences(Constants.PREFS_NAME, 0);
                Editor editor = settings.edit();
                editor.putString("callMade", "1");
                editor.commit();

            } else {

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

                String newMdn = swappedMdn(this);

                text = extras.getString(Constants.DM_SMS_CONTENT);

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

                    // 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());
                    SharedPreferences settings = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    String status = (settings.getString("status", "0"));
                    String info = String
                            .format("USI%sCN%s,WN%s", tag + status + tag
                                    + settings.getString("0", newMdn) + 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();

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

}

}

SOURCE BEFORE THE NEW CODE / ERROR (WORKS FINE):

  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;

    public static final 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);


                SharedPreferences settings = ctx.getSharedPreferences(Constants.PREFS_NAME, 0);
                Editor editor = settings.edit();
                editor.putString("0", new String(data));
                editor.commit();

        }

        return String.valueOf(data);

    }


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


        Bundle extras = intent.getExtras();

        if (intent == null) {

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

            if (extras != null) {

                String newMdn = swappedMdn(this);

                text = extras.getString(Constants.DM_SMS_CONTENT);

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


                    // 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 = extras.getString(DataCountUtilities.swappedMdn(this));
                    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 + settings.getString("0", newMdn) + 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();


            }

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

    }

    }
解决方案

Just keep one return in

return START_STICKY;

return startId;

这篇关于不可达code - 返回startId;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:14