Android requestLocationUpdates()和removeUpdates(),无法使用TimerTask在单独的线程中处理
我在我的服务中使用Timer和TimerTask,并使用@Service-onCreate()方法安排GPS定期检查 我的服务类中的代码段:Android requestLocationUpdates()和removeUpdates(),无法使用TimerTask在单独的线程中处理,android,multithreading,thread-safety,android-service,android-location,Android,Multithreading,Thread Safety,Android Service,Android Location,我在我的服务中使用Timer和TimerTask,并使用@Service-onCreate()方法安排GPS定期检查 我的服务类中的代码段: //class inside service class class AllGPSTimer extends TimerTask { public void run() { if (!canGetLocation) stopLocationRequest(); else requestLocation(); } } // Serv
//class inside service class
class AllGPSTimer extends TimerTask {
public void run() {
if (!canGetLocation) stopLocationRequest();
else requestLocation();
}
}
// Service class method
public void requestLocation() {
try {
locListener = new mylocationlistener();
locationManager.requestLocationUpdates(provider, 0, 0, locListener);// Error happens at this line
}
catch (Exception e) {
e.printStackTrace();
}
}
public void stopLocationRequest() {
try {
locationManager.removeUpdates(locListener);// And also it happens at this line
}
catch (Exception e) {
e.printStackTrace();
}
}
我得到这个错误:
Error :
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:139)
at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:137)
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:708)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:630)
at com.mobile.sales.GPSService.requestLocation(GPSService.java:146)
at com.mobile.sales.GPSService$AllGPSTimer.run(GPSService.java:140)
at java.util.Timer$TimerImpl.run(Timer.java:289)
错误:
java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序
android.os.Handler.(Handler.java:121)
在android.location.LocationManager$ListenerTransport$1。(LocationManager.java:139)
位于android.location.LocationManager$ListenerTransport。(LocationManager.java:137)
在android.location.LocationManager.\u请求LocationUpdate(LocationManager.java:708)
位于android.location.LocationManager.RequestLocationUpdate(LocationManager.java:630)
位于com.mobile.sales.GPSService.requestLocation(GPSService.java:146)
在com.mobile.sales.GPSService$AllGPSTimer.run(GPSService.java:140)
在java.util.Timer$TimerImpl.run(Timer.java:289)
从错误中,我猜测这个requestLocationUpdates()和
removeUpdates()只能从服务类的主线程使用,不能从主线程的派生线程使用。我猜对了吗
那么,在服务类的活动类中是否有类似runOnUiThread(runnable)的方法
有谁能帮我处理location API方法和计时器任务的派生线程吗?如果我猜错了,也请告诉我
谢谢大家! 公共类GpsManager
{
}看看这个问题@southerton:我看到了整个过程和源代码,这是否意味着我们不能在使用LocationAPI时使用计时器???你能简要介绍一下是否有任何方法吗???嗨@Pradeepraj你不应该在线程内调用任何硬件检查控件。我们应该使用Handler。下面的链接将帮助您@拉杰什:谢谢拉杰什,这很有用。。但是我试过定时器里面的KeyguardManager它工作正常。。但就位置而言,它不起作用。。正如Southerton评论的LocationManager在内部使用Looper和Handler一样,我正在检查代码片段,以便以这种方式处理LocationManager
Error :
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:139)
at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:137)
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:708)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:630)
at com.mobile.sales.GPSService.requestLocation(GPSService.java:146)
at com.mobile.sales.GPSService$AllGPSTimer.run(GPSService.java:140)
at java.util.Timer$TimerImpl.run(Timer.java:289)
public GpsManager()
{
// TODO Auto-generated constructor stub
}
LocationManager lm;
LocationListener ll;
public void Start(Context context)
{
lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
isactive = true;
handler=new Handler(){
@Override
public void handleMessage(Message msg)
{
if(isactive)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
super.handleMessage(msg);
}
};
ll = new MyLocationListener(context, lm,handler);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
Handler handler;
public boolean isactive;
public void Stop()
{
lm.removeUpdates(ll);
isactive = false;
}
class MyLocationListener implements LocationListener
{
Context _con;
LocationManager _lm;
Handler _handler;
public MyLocationListener(Context con, LocationManager lm,Handler h)
{
_lm = lm;
_con = con;
_handler=handler;
}
@Override
public void onLocationChanged(Location location)
{
if (location != null)
{
Toast.makeText(
_con,
"lon = " + location.getLongitude() + ". lat = "
+ location.getLatitude(), Toast.LENGTH_SHORT)
.show();
lm.removeUpdates(this);
Thread th=new Thread(new Runnable()
{
@Override
public void run()
{
Message msg=Message.obtain();
_handler.sendMessageDelayed(msg,1000);
}
});
th.run();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText(_con, "status change " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText(_con, "provider enable " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText(_con, "provider disable " + provider,
Toast.LENGTH_SHORT).show();
}
}