我创建了一个服务,并且在activity中启动了它,它OnCreate,OnStart方法都被调用了,可以正常运行,但不知道为什么运行一到两个小时之后就会自动停止,我看了一下log,OnCreate方法会被再次调用,但OnStart方法就没有再被调用,所以导致服务就像停止了一样。在应用程序管理页面-》正在运行中的服务中还是可以看到我创建的服务,显示还在运行。但其实已经不work,服务中创建的线程已经不工作了。请问大家这是怎么回事?
我创建的服务也很简单,下面是服务的代码:package my.testservice;
import java.util.Date;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class GSService extends Service { private boolean threadDisable=false;
private int count=0;
private SDFileMan mSDFFileMan = new SDFileMan("MyLog.txt");
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
super.onCreate();
String msg = "The service is created! DateTime:" + DateTimeUtil.FormatDateToString(new Date());
Log.e( "GSService" , msg);
mSDFFileMan.saveAppendLine(msg);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
String msg = "The service is Started! DateTime:" + DateTimeUtil.FormatDateToString(new Date());
Log.e( "GSService" , msg);
mSDFFileMan.saveAppendLine(msg);
new Thread(new Runnable(){ @Override
public void run() {
while (!threadDisable) {
try {
Thread.sleep( 3000 );
} catch (InterruptedException e) {}
count ++ ;
Log.e( "GSService" , " Count is " + count);
mSDFFileMan.saveAppendLine("GSService is running, Count= " + count +"! DateTime:" + DateTimeUtil.FormatDateToString(new Date()));
}
}}).start();
}
@Override
public void onDestroy() {
super .onDestroy();
threadDisable = true;
String msg = "The service is Destroyed! DateTime:" + DateTimeUtil.FormatDateToString(new Date());
Log.e( "GSService" , msg);
mSDFFileMan.saveAppendLine(msg);
}
}
我创建的服务也很简单,下面是服务的代码:package my.testservice;
import java.util.Date;import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class GSService extends Service { private boolean threadDisable=false;
private int count=0;
private SDFileMan mSDFFileMan = new SDFileMan("MyLog.txt");
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
super.onCreate();
String msg = "The service is created! DateTime:" + DateTimeUtil.FormatDateToString(new Date());
Log.e( "GSService" , msg);
mSDFFileMan.saveAppendLine(msg);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
String msg = "The service is Started! DateTime:" + DateTimeUtil.FormatDateToString(new Date());
Log.e( "GSService" , msg);
mSDFFileMan.saveAppendLine(msg);
new Thread(new Runnable(){ @Override
public void run() {
while (!threadDisable) {
try {
Thread.sleep( 3000 );
} catch (InterruptedException e) {}
count ++ ;
Log.e( "GSService" , " Count is " + count);
mSDFFileMan.saveAppendLine("GSService is running, Count= " + count +"! DateTime:" + DateTimeUtil.FormatDateToString(new Date()));
}
}}).start();
}
@Override
public void onDestroy() {
super .onDestroy();
threadDisable = true;
String msg = "The service is Destroyed! DateTime:" + DateTimeUtil.FormatDateToString(new Date());
Log.e( "GSService" , msg);
mSDFFileMan.saveAppendLine(msg);
}
}
i.setClass(MyServiceTestActivity.this, GSService.class);
MyServiceTestActivity.this.startService(i);
service如果你想它运行的时间很长的话,最好是把它搞成foreground service,这样就不会给系统回收
再试试。
public void onCreate() {
super.onCreate();
setForeground(true);
}
已经说过了,搞一个forgroundservice,看看apidemo中,怎么写吧。