我不确定我到底改变了什么 - 但突然间我得到一个“java.lang.StackOverflowError”,从我读到的内容是由于无限递归。由于我对 Java/Android 开发相对较新,我以前从未遇到过这种情况,因此我很难准确地确定这个递归问题的产生位置。
任何帮助是极大的赞赏。
- 阿曼尼·斯旺
活动来源:
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 Intent getIntent() {
// TODO Auto-generated method stub
return null;
}
// compat to support older devices
@Override
public void onStart(Intent intent, int startId) {
onStartCommand(intent, 0, startId);
// @Override
// public int onStartCommand(Intent intent, int flags, int startId) {
//display in long period of time
Toast.makeText(getApplicationContext(), "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");
}
}
@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("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);
}
private Timer timer = new Timer();
private final long PERIOD = 1000 * 15; // x min
private final long DELAY_INTERVAL = 0; // x Seconds
@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);
}
}