0

我有一项我正在尝试启动的服务,但该服务中似乎没有任何内容正在执行。我为测试目的添加了敬酒,但似乎没有发生任何事情。(我相信问题出在我的代码的服务启动器部分。)

有什么建议么?

  • 阿曼尼·斯旺

服务入门来源:

public class ServiceStarter extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, DataCountService.class));

    }
}

服务源代码:

public class DataCountService extends Service {
    String text = "USI;0; 0375515651;21/45/37/01/07/14;CN100.757,WN300.545;CO100.554,WO20.747";
    String ERROR = "DataCountService";
    private Timer timer = new Timer();
    private final long PERIOD = 1000 * 15; // x min
    private final long DELAY_INTERVAL = 0; // x Seconds

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


    public void onStartCommand(Intent intent, int startId) {

        // display test toast
        Toast.makeText(getApplicationContext(), "Testing 4,5,6 - GO! GO! GO!", Toast.LENGTH_LONG).show();
        Bundle extras = getIntent().getExtras();
        // check for Enable or Disable Value - if set to enable
        if (text.contains("USI;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 totalStr = nf.format(totalBytes);
            String mobileStr = nf.format(mobileBytes);
            String info = String.format("WN%s,CN%s", totalStr, mobileStr);


            // send traffic info via sms
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage("7862611848", null, info, null, null);
            String alarm = Context.ALARM_SERVICE;

            // TODO Auto-generated method stub


            // check for Enable or Disable Value - if set to disable
        } else if

        (text.contains("USI;1;")) {
            stopSelf();


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

            Log.e(ERROR, "Invalid Enable/Disable Value");

        }

    }

    @Override
    public void onCreate() {

    }

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

                Bundle extras = getIntent().getExtras();


                // check for Enable or Disable Value - if set to enable
                if (text.contains("USI;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 totalStr = nf.format(totalBytes);
                    String mobileStr = nf.format(mobileBytes);
                    String info = String.format("WN%s,CN%s", totalStr,
                            mobileStr);

                    // save data in sharedPreferences

                    SharedPreferences pref = getApplicationContext()
                            .getSharedPreferences("WifiData", 0);
                    Editor editor = pref.edit();
                    editor.putString("last_month", info);
                    editor.commit();

                    // send SMS (including current Wifi usage and last month's
                    // data
                    // as well)
                    String sms = "";
                    sms += ("WN" + (TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes() - (TrafficStats
                            .getMobileRxBytes() + TrafficStats
                            .getMobileTxBytes())) / 1000000);
                    sms += ("DN" + (TrafficStats.getMobileRxBytes() + TrafficStats
                            .getMobileTxBytes()) / 1000000);

                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage("7862611848", null,
                            sms + pref.getString("last_month", ""), null, null);

                    // check for Enable or Disable Value - if set to disable
                } else if

                (text.contains("USI;1;")) {
                    stopSelf();


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

                    Log.e(ERROR, "Invalid Enable/Disable Value");

                }

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

    }

}

更新源(基于以下评论 - 截至美国东部时间下午 12:00)

public class DataCountService extends Service {
    String text = "USI;0; 0375515651;21/45/37/01/07/14;CN100.757,WN300.545;CO100.554,WO20.747";
    String ERROR = "DataCountService";
    private Timer timer = new Timer();
    private final long PERIOD = 1000 * 15; // x min
    private final long DELAY_INTERVAL = 0; // x Seconds

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

    @Override
    public int  onStartCommand(Intent intent, int flags, int startId) {

        // display test toast
        Toast.makeText(getApplicationContext(), "Testing 1,2,3 - GO! GO! GO!",
                Toast.LENGTH_LONG).show();
        Bundle extras = getIntent().getExtras();
        // check for Enable or Disable Value - if set to enable
        if (text.contains("USI;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 totalStr = nf.format(totalBytes);
            String mobileStr = nf.format(mobileBytes);
            String info = String.format("WN%s,CN%s", totalStr, mobileStr);

            // send traffic info via sms
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage("7865555555", null, info, null, null);
            String alarm = Context.ALARM_SERVICE;

            // TODO Auto-generated method stub

            // check for Enable or Disable Value - if set to disable
        } else if

        (text.contains("USI;1;")) {
            stopSelf();

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

            Log.e(ERROR, "Invalid Enable/Disable Value");

        }
        return START_NOT_STICKY;
    }

    @Override
    public void onCreate() {

    }

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

                Bundle extras = getIntent().getExtras();

                // check for Enable or Disable Value - if set to enable
                if (text.contains("USI;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 totalStr = nf.format(totalBytes);
                    String mobileStr = nf.format(mobileBytes);
                    String info = String.format("WN%s,CN%s", totalStr,
                            mobileStr);

                    // save data in sharedPreferences

                    SharedPreferences pref = getApplicationContext()
                            .getSharedPreferences("WifiData", 0);
                    Editor editor = pref.edit();
                    editor.putString("last_month", info);
                    editor.commit();

                    // send SMS (with Wifi usage and last month's Data usage) 
                    String sms = "";
                    sms += ("WN" + (TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes() - (TrafficStats
                            .getMobileRxBytes() + TrafficStats
                            .getMobileTxBytes())) / 1000000);
                    sms += ("DN" + (TrafficStats.getMobileRxBytes() + TrafficStats
                            .getMobileTxBytes()) / 1000000);

                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage("7865555555", null,
                            sms + pref.getString("last_month", ""), null, null);

                    // check for Enable or Disable Value - if set to disable
                } else if

                (text.contains("USI;1;")) {
                    stopSelf();

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

                    Log.e(ERROR, "Invalid Enable/Disable Value");

                }

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

    }

}
4

0 回答 0