며칠 전 앱에 끔찍한 문제가있었습니다. 내 앱이 백그라운드에서 특정 작업을 다시 시작했습니다. 언젠가 나는 모든 문제가 내 서비스의 onStartCommand()
방법에서 비롯된 것으로 나타났습니다. 예를 들어 URL, 이름, 경로 등의 정보를 제공하여 서비스를 시작하여 다운로드 작업을 시작하면 모두 잘 작동합니다. 언젠가 내가 다른 앱을 쓰고 나서. 나는 같은 다운로드 작업이 다시 시작된다는 축하를받습니다. 따라서, 서비스에서의 모든 문제를 분명히 알 수 있습니다. 서비스를 조금 검색했지만 서비스가 다시 시작되는 이유를 혼동합니다. 누구든지 올바른 정보를 제공하면 도움이 될 것입니다. 여기에 서비스 코드 :백그라운드에서 Android 서비스가 다시 시작됩니다.
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();
}
}