异步任务不能正常工作(doInBackground未执行),当服务运行在后台,安卓 [英] Async task does not work properly (doInBackground not executing) when service runs in background, Android

查看:888
本文介绍了异步任务不能正常工作(doInBackground未执行),当服务运行在后台,安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,有时异步任务不能正常工作,其实它的 doInBackground()方法不会被调用,出现这种情况主要是当背景,该活动的任何服务运行。 例如,当音乐在后台与服务运行时,异步任务不解析XML的背景,其doInBackground不工作的时间和进度对话框或进度不停地打转。

I noticed that sometimes Async task does not work properly , Actually its doInBackground() method does not get called , this happens mostly when any service run in background for that activity. For Example , when music runs in background with service, the Async task does not parse XML in background as its doInBackground does not work that time and the progress Dialog or progressBar kept spinning.

我看了一些文章 AsyncTask.THREAD_POOL_EXECUTOR 可以在这些问题,如帮助:

I read in few articles that AsyncTask.THREAD_POOL_EXECUTOR can help in these issues like :

if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
    new Test().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    new Test().execute();
}

没有帮助在我的情况。上述implmentation后有同样的问题。

but that did not help in my case. Having the same issue after the above implmentation.

下面我给刚刚有点我的例子code明白我在做什么::

Here I am giving just a bit of my sample code to understand what I am doing::

public class TestAct extends Activity {

    ImageButton play,forward,backward;  
    private ListView mList;
    // many more variables

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);

        //binding the service here
        // start service is called

        init();
    }

    private void init(){

        play=(ImageButton)findViewById(R.id.playBtn);
        forward=(ImageButton)findViewById(R.id.forward);
        backward=(ImageButton)findViewById(R.id.backward);  
        mList=(ListView)findViewById(R.id.list);

        new GetData().execute();

        play.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // calling the play() method of ServiceConnection here
            }
        });

            // adding header to Listview
            // other code and click listeners

    }

    class GetData extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            // starting the progress Bar
            // initializing the Arraylist,Maps etc
        }

        @Override
        protected Void doInBackground(Void... params) {
            //parsing the XML here
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            // stop the ProgressBar 
            // Updating my UI here
                    // setting Adapter for ListView 
        }   
    }
}

这工作得很好,但通常挂在服务中backgound运行(我的意思是,当音乐响起的背面)。

This works fine generally but hangs when Service runs in backgound (I mean when music is playing in back).

我没有得到背后的异步任务这一问题的确切原因。 将mannual线程执行的帮助在这种情况下... ??

I am not getting the exact reason behind this problem of Async task. Will mannual thread implementation help in this case ...??

嗯,我认为这个问题是因为服务运行在主线程中运行,所以当,它阻止我的AsyncTask运行......所以,我认为如果我们可以在后台线程中运行服务则可以帮助。这就是为什么我试图IntentService运行在单独的线程服务,但我很怀疑......如果IntentService可以运行无限期类似服务......也IntentService块AsyncTask的几十倍。 所以我dont't认为其100%的完美解决方案,这样那样的问题。

Well, I think the problem is because "Service runs in main thread so when it runs, it blocks my AsyncTask to run"... So I think If we can run Service in background thread then that can help . Thats why I tried IntentService for running service in separate thread but I am in doubt... if IntentService can run for indefinite time similar to Service ... and Also IntentService blocks AsyncTask few times. So I dont't think its 100% perfect solutions for this kind of problem.

谁能帮我理清这个问题,了解完整的场景。

Can anyone help me to sort out this problem and understand the complete scenario.

在此先感谢。

推荐答案

下面是一个提示,我终于如何解决我的问题::

Here is a hint, How I finally solved my Problem ::

1)我用的 IntentService,而不是服务作为服务运行在mainThread而IntentService运行在一个单独的线程比mainThread,以确保我的后台服务,不会影响我的当前任务。另外,我使用AIDL我的UI和后台线程之间的通信(这是已经工作的服务,所以没有什么新在这部分)。

1) I used IntentService instead of Service as Service runs in mainThread while IntentService runs in a separate Thread than mainThread to make sure that my background Service does not effect my current task . Also , I am using AIDL for communication between my UI and background Thread (this was already working for Service , so nothing new in this part).

2)我用的无痛线程,而不是AsyncTask的,我中断线程的onDestroy()的方法,以确保该线程不会无限期地持续下去。

2) I used painless thread instead of AsyncTask, I interrupt the thread in onDestroy() method to make sure that the Thread does continue indefinitely.

应用程序似乎工作比以前好多了。

App seems to perform much better than Earlier now.

希望这会帮助别人太:)

Hope this will help others too :)

这篇关于异步任务不能正常工作(doInBackground未执行),当服务运行在后台,安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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