Byteswapping an MDN within a service in Android - Impossible?

微笑、不失礼 提交于 2019-12-13 04:23:28

问题


I have an activity which will byteswap the MDN of a device however I need to implement this in a service however I cannot seem to do so. Every time I attempt to add the code to my service I get different errors depending on the method I use and I was told by another developer that I'll need to implement an activity - but this is not an option (it needs to run in the background) so I need to find a method of byteswapping the MDN within either my existing service or another service (or an invisible activity [if such a thing exists])

Functional Byteswapping Activity (which needs to be executed as a service)

 public class DeviceManagerUtilities {

//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;
    }
    return String.valueOf(data); 
}

}

SERVICE SOURCE CODE:

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

        }

    }






ATTEMPTED COMBINATION OF THE TWO: 
-------------------


public class DeviceManagerUtilities {

    //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;
        }
        return String.valueOf(data); 
    }

}

ERRORS

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

来源:https://stackoverflow.com/questions/17302281/byteswapping-an-mdn-within-a-service-in-android-impossible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!