2013-09-02 5 views
-1

며칠 전 앱에 끔찍한 문제가있었습니다. 내 앱이 백그라운드에서 특정 작업을 다시 시작했습니다. 언젠가 나는 모든 문제가 내 서비스의 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(); 
    } 

} 

답변

1

시작은 의도

//to register the services 
IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction(ServicesTemplate.MY_ACTION); 
registerReceiver(myReceiver, intentFilter); 
//to start this services 
Intent myIntent = new Intent(Mainactivity.this, Yourserviceclassname.class); 
startService(myIntent); 

당신이

0

시작 완료 다운로드 후 서비스를 중지 할 의도

Intent oIntent = new Intent(Mainactivity.this, Yourserviceclassname.class); 
stopService(oIntent); 

를 중지하려면 어떤 서비스 든, 일단 목적이 달성되면 서비스를 중지해야합니다. 두 가지 방법으로 서비스를 중지 할 수 있습니다.

1. stopService() 방법을 통해 서비스를 중지 할 수 있습니다. startService (intent) 메소드를 얼마나 자주 호출했는지에 관계없이 stopService() 메소드를 한 번 호출하면 서비스가 중지됩니다.

2. 서비스는 stopSelf() 메서드를 호출하여 종료 할 수 있습니다. 이것은 일반적으로 서비스가 작업을 마친 경우에 수행됩니다.

귀하의 경우에는 파일 대신 IntentServices으로 서비스 대신 Intentservice을 사용하시는 것이 좋습니다. 서비스에 대한 자세한 정보는 참조 할 수 있습니다 들어

// Start the service 
public void startService(View view) { 
    startService(new Intent(this.your_activity, Your_Service.class)); 
} 

// Stop the service 

public void stopService(View view) { 

    stopService(new Intent(this.your_activity, Your_Service.class)); 
} 

,
  • Tutorial on Services By Vogella 2. Android service
    1. 관련 문제