Android服务在后台一次又一次地重新启动

时间:2013-09-02 08:18:17

标签: android android-service

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

}

2 个答案:

答案 0 :(得分: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);

下载完成后你必须停止服务

答案 1 :(得分:0)

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

1.您可以通过stopService()方法停止服务。无论您调用startService(intent)方法的频率如何,对stopService()方法的一次调用都会停止服务。

2.服务可以通过调用stopSelf()方法终止自身。这通常在服务完成其工作时完成。

在您的情况下,我建议您使用Intentservice代替服务作为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. Tutorial on Services By Vogella 2. Android service