在Android的同时运行多个服务 [英] running multiple services at one time in android

查看:190
本文介绍了在Android的同时运行多个服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发民与多个选项卡Android应用程序。我的卡有以下几种。

I am devloping an android application with multiple tabs. My tabs have the following.

1选项卡 - 它显示档案在网格视图一本杂志。其目的是,当我点击一个网格视图项目,我需要从一个网站下载一个大文件。为此,我已经创建了一个具有异步任务将下载文件的服务。我已经表明了code。

1st tab - It shows archives for a magazine in a grid view. The intention is to when I tap a grid view Item, I would need to download a large file from a web site. For this I have created a service that has an ASYNC task that would download the file. I have shown the code.

 public class DownloadArchivesService extends BaseService {
private static final String TAG = "DownloadArchivesServices";
private static final String STOP = "stop_modificator";
public static final String ALREADY_RUNNING= "DownloadArchivesService_Already_Running";
public  static final String ARCHIVES_PERCENTAGE_PROGRESS ="Archives_Percentage_Progress";
public  static final String UPDATE_ARCHIVES_PROGRESS = "Update_Archives_Progress";

 @Override
public void onCreate() {
            getPreferences().edit().putBoolean(ALREADY_RUNNING,   true).commit();
    application = (JfwApplication)this.getApplicationContext();
    context = this;
    session = application.getSession();
    calendar = Calendar.getInstance();
    dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    magazineManager = new MagazineManager(this);

    //magazine = new Magazine(fileName, fileName, "", "", this);


    new AsyncTask<String, Integer, String>() {

    NumberFormat formater = NumberFormat.getPercentInstance(Locale.getDefault());
    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        //  I do my download here       }

    @Override
    protected void onProgressUpdate(Integer... p) {
        if (isCancelled()) {
            return;
        }
        //I udpate my progress bar here     }

    @Override
    protected void onPostExecute(String result) {

        Intent intentInvalidate = new Intent(ArchivesActivity.GRID_INVALIDATE);
        sendBroadcast(intentInvalidate);

        super.onPostExecute(result);
    }
}.execute();

super.onCreate();


}

当我浏览到我的第二个选项卡,我需要从网站上下载了几张照片,并在屏幕上显示。我已经写类似于上面的服务 - DownloadImagesService。

When I navigate to my second tab, I would need to download a few photos from the website and display it on the screen. I have written a service similar to the above - DownloadImagesService.

但是,当上述DownloadArchivesService运行,(即,当我有问题
 窃听调用DownloadArchivesService)并导航到第二个选项卡下载照片网格视图项目时,DownloadImageService(下载照片)服务没有调用,它等待,直到DownloadArchivesService完成。这将使用户等待的第一个服务完成之前的某个时候。据我所知,服务将在主线程中运行,一个服务需要等待一个已经执行的服务来执行。
有周围的方式以同时不必等待previously excecuting服务运行不同的服务(或背景任务)。

But the problem is when the above mentioned DownloadArchivesService is running,(i.e., when I had tapped a grid view item to invoke the DownloadArchivesService) and navigate to the second tab to download the photos,The DownloadImageService(The service to download photos) is not invoked and it waits till the DownloadArchivesService is completed. This would make the user to wait for sometime before the first service completes. I understand that the services would run in main thread and one service would need to wait for an already executing service to execute. Is there a way around to run different services(or background tasks) concurrently without waiting for a previously excecuting services.

我没有通过对服务的各种文件,意图服务和线程。但我不能undertand上的Andorid如何处理并发服务这个特殊的事情。

I did go through various documents on the Services, Intent services and threads. But I could not undertand this particular thing on how andorid handles concurrent services.

我将不胜AP preciate有这方面的帮助。请让我知道如果我的解释是不明确的。

I would greatly appreciate any help on this. Please let me know If My explanation is not clear.

感谢。

推荐答案

的AsyncTask的设计是围绕主题和Handler一个辅助类,并不构成通用线程框架。 AsyncTasks最好应(至多几秒钟。)可用于短期操作。如果您需要保持对长时间运行的线程,强烈建议您使用的java.util.concurrent中pacakge如提供的各种API遗嘱执行人,并ThreadPoolExecutor的FutureTask提供。

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.

在首次推出,AsyncTasks被连续在一个后台线程执行。与DONUT开始,这个改变为线程允许多个任务,以在并行地操作的池。 蜂窝开始,任务是在单个线程执行,以避免因并行执行常见的应用程序错误。

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

如果你真正想要的并行执行,可以调用executeOnExecutor(java.util.concurrent.Executor,对象[])与THREAD_POOL_EXECUTOR。

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

http://developer.android.com/reference/android/os/ AsyncTask.html

我会建议你使用执行人为此

I would suggest you to use executor for this purpose

http://developer.android.com/reference/java/ UTIL /并行/ Executor.html

这篇关于在Android的同时运行多个服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆