我正在尝试启动服务,但服务中的任何内容似乎都没有执行。我为测试目的添加了吐司,但似乎没有发生任何事情。 (我认为问题出在我的代码的服务启动程序部分。)
有什么建议吗?
服务起始源:
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);
}
}