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



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

启动意向

//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.您可以通过stopService()方法停止服务。无论您多么频繁地调用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服务

相关内容

最新更新