几天前,我的应用程序遇到了一个可怕的问题。我的应用程序在后台一次又一次地重新启动特定工作。一段时间后,我发现所有问题都来自我的服务onStartCommand()
方法。例如,我通过启动服务来启动下载任务,并为其提供 URL、名称、路径等信息,并且一切正常。一段时间后,当我在任何其他应用程序上时。我得到了重新启动相同下载任务的祝酒词。所以,很明显,服务中的所有问题。我对服务进行了一点搜索,但对服务重启的原因感到困惑。如果有人提供正确的信息,那将非常有帮助。这是服务代码:
public class DownloadService extends Service {
private DownloadManager mDownloadManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mDownloadManager = new DownloadManager(this, (FatherApplication) getApplication());
}
protected boolean addDownload(String fileU, String fileP, String fileN){
try{
if(mDownloadManager != null){
mDownloadManager.addTask(fileU, fileP, fileN);
}
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
FatherApplication mApp =(FatherApplication) getApplication();
/** I am not sure the Global has been intilized or not, So i need to check is the global data is intilized or
* has been null. if null intilized it by calling GlobalData.getIntense(). */
if(mApp.getGlobalData() == null){
mApp.setGlobalData();
}
if (mDownloadManager == null) {
mDownloadManager = new DownloadManager(this, (FatherApplication) getApplication());
}
String action = intent.getAction();
int type = -1;
if(action.equals( SystemIntent.INTENT_ACTION_START_SARVICE )){
type= intent.getIntExtra(SystemIntent.TYPE, -1);
}
/* --- Add --- */
if(type != -1 && type == SystemIntent.Types.ADD){
String fname= intent.getStringExtra(SystemIntent.FILE_NAME);
String fpath= intent.getStringExtra(SystemIntent.FILE_PATH);
String furl = intent.getStringExtra(SystemIntent.FILE_URL);
this.addDownload(furl,fpath,fname);
}
/* --- paused --- */
if(type != -1 && type == SystemIntent.Types.PAUSE){
String fname= intent.getStringExtra(SystemIntent.FILE_NAME);
String fpath= intent.getStringExtra(SystemIntent.FILE_PATH);
String furl = intent.getStringExtra(SystemIntent.FILE_URL);
if(this.mDownloadManager.isThatARunningTask(furl,fname,fpath))
this.mDownloadManager.pauseTask(furl,fname,fpath);
else Toast.makeText(this,"That's not running task.",2).show();
}
/* --- deleted --- */
if(type != -1 && type == SystemIntent.Types.DELETE){
String fname= intent.getStringExtra(SystemIntent.FILE_NAME);
String fpath= intent.getStringExtra(SystemIntent.FILE_PATH);
String furl = intent.getStringExtra(SystemIntent.FILE_URL);
this.mDownloadManager.deleteTask(furl,fname,fpath);
}
/* --- source deleted --- */
if(type != -1 && type == SystemIntent.Types.DELETE_SOURCE){
String fname= intent.getStringExtra(SystemIntent.FILE_NAME);
String fpath= intent.getStringExtra(SystemIntent.FILE_PATH);
String furl = intent.getStringExtra(SystemIntent.FILE_URL);
this.mDownloadManager.deleteTask(furl,fname,fpath);
}
/* --- restarted --- */
if(type != -1 && type == SystemIntent.Types.RESTART){
String fname= intent.getStringExtra(SystemIntent.FILE_NAME);
String fpath= intent.getStringExtra(SystemIntent.FILE_PATH);
String furl = intent.getStringExtra(SystemIntent.FILE_URL);
this.mDownloadManager.restartTask(furl,fname,fpath);
}
/* --- resumed --- */
if(type != -1 && type == SystemIntent.Types.RESUME){
String fname= intent.getStringExtra(SystemIntent.FILE_NAME);
String fpath= intent.getStringExtra(SystemIntent.FILE_PATH);
String furl = intent.getStringExtra(SystemIntent.FILE_URL);
if(!this.mDownloadManager.isThatARunningTask(furl,fname,fpath))
this.mDownloadManager.resumeTask(furl,fname,fpath);
else Toast.makeText(this,"That's a running task.",2).show();
}
return this.START_REDELIVER_INTENT;
}
/** I going to destroy the service , shibo you should do your importent works. */
@Override
public void onDestroy(){
this.mDownloadManager.close();
super.onDestroy();
}
}