-1

几天前,我的应用程序遇到了一个可怕的问题。我的应用程序在后台一次又一次地重新启动特定工作。一段时间后,我发现所有问题都来自我的服务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();
    }

}
4

2 回答 2

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);

停止意图

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

下载完成后必须停止服务

于 2013-09-02T08:35:14.460 回答
0

当您启动任何服务时,一旦达到目的,您也需要停止它。您可以通过两种方式停止服务。

stopService()1.您可以通过该方法停止服务。无论您多么频繁地调用 startService(intent) 方法,一次调用 stopService() 方法都会停止服务。

2.服务可以通过调用stopSelf()方法自行终止。如果服务完成他的工作,通常会这样做。

在您的情况下,我建议您使用Intentservice而不是 Service 作为IntentServices一次性任务,例如下载文件。

// 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));
}

有关服务的更多信息,您可以参考,

  1. Vogella 的服务教程2. Android 服务
于 2013-09-02T08:55:26.270 回答