启动服务时java.lang.StackOverflower错误
我不确定我到底改变了什么-但突然我得到了一个“java.lang.StackOverflowerr”,从我正在阅读的内容来看,这是由于内隐递归。由于我对Java/Android开发相对较新,我以前从未遇到过这一点,因此我很难准确地指出递归问题是从哪里产生的 非常感谢您的帮助启动服务时java.lang.StackOverflower错误,java,android,stack-overflow,fatal-error,Java,Android,Stack Overflow,Fatal Error,我不确定我到底改变了什么-但突然我得到了一个“java.lang.StackOverflowerr”,从我正在阅读的内容来看,这是由于内隐递归。由于我对Java/Android开发相对较新,我以前从未遇到过这一点,因此我很难准确地指出递归问题是从哪里产生的 非常感谢您的帮助 阿曼尼·斯旺 活动来源: 服务来源: 不要从onStart()调用onStartCommand(),因为这将给您无限递归。服务永远不应该调用onStartCommand(),除非在其超类(super.onStartCom
- 阿曼尼·斯旺
不要从
onStart()
调用onStartCommand()
,因为这将给您无限递归。服务永远不应该调用onStartCommand()
,除非在其超类(super.onStartCommand(…)
)上重写onStartCommand()
另外,由于
onStart()
已经被弃用三年多了,请重写onStart命令()
,而不是onStart()
您可以编辑您的问题来解释您正在尝试做什么吗?您还可以发布异常的开始吗?没有堆栈跟踪,很难知道在哪里搜索错误。
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);
}
}