如何调用" requestLocationUpdates"从AsyncTask的 [英] How to call "requestLocationUpdates" from a AsyncTask

查看:98
本文介绍了如何调用" requestLocationUpdates"从AsyncTask的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AsyncTask的地方检查加速度计的数据。当我曾经检测到加速度计是给较大的值,我需要启动GPS记录。

 私人的LocationManager mlocManager;
私人GPSLocationListener mlocListener;
..
mlocManager =(的LocationManager)context.getSystemService(context.LOCATION_SERVICE);
mlocListener =新GPSLocationListener();
..
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,100,mlocListener);
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10000,100,mlocListener);

如何使这个运行?我得到的错误是

 在执行doInBackground发生错误()
不能内螺纹创建处理程序,也没有所谓的活套。prepare()


解决方案

的AsyncTask不弯针线,因此该问题上运行,你可能想,因为他们与弯针线运行,因此这样的错误不会发生使用的处理程序

I have a AsyncTask where it checks for the Accelerometer data. When ever i detects that Accelerometer is giving larger values i need to start the GPS recording.

private LocationManager mlocManager;
private GPSLocationListener mlocListener;
..
mlocManager = (LocationManager)context.getSystemService(context.LOCATION_SERVICE);
mlocListener = new GPSLocationListener();
..
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 100, mlocListener); 
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 100, mlocListener); 

How to make this running? The errors i get are

error occured while executing doInBackground()
Can't create handler inside thread that has not called Looper.prepare()

解决方案

AsyncTask doesn't run on the Looper thread and hence the problem, You may want to use Handlers as they run with the Looper thread and hence such errors wont occur.

这篇关于如何调用" requestLocationUpdates"从AsyncTask的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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