android异步线程未执行,关于多线程:当服务在后台运行时,Android异步任务无法正常运行(doInBackground未执行)...

博客讨论了在Android服务后台运行时,如播放音乐服务,导致AsyncTask的doInBackground()方法不执行的问题。作者尝试了使用AsyncTask.THREAD_POOL_EXECUTOR,但问题仍然存在。解决方案包括使用IntentService替代Service,以确保服务在单独的线程中运行,以及使用无痛线程代替AsyncTask。此外,还建议在服务中创建包含Looper的HandlerThread进行主线程和后台线程间的通信。
摘要由CSDN通过智能技术生成

我注意到有时Async任务无法正常运行,实际上它的doInBackground()方法没有被调用,这种情况主要发生在该活动在后台运行任何服务时。

例如,当音乐在带有服务的后台运行时,Async任务不会在后台解析XML,因为它的doInBackground当时不起作用,并且进度Dialog或progressBar一直旋转。

我读过几篇文章,AsyncTask.THREAD_POOL_EXECUTOR可以帮助解决以下问题:

if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {

new Test().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

} else {

new Test().execute();

}

但这对我的情况没有帮助。在上述实施后存在相同的问题。

在这里,我仅提供一些示例代码来了解我在做什么:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值