从IntentService得到一个位置上时,一个死线程发送消息到一个处理 [英] Sending message to a Handler on a dead thread when getting a location from an IntentService
问题描述
我的应用程序需要定期位置修正,即使手机没有清醒。 要做到这一点,我使用IntentService与Commonsware慷慨地提供了模式。 https://github.com/commonsguy/cwac-wakeful
My app needs location fixes on regular basis, even when the phone is not awake. To do this, I am using IntentService with the pattern generously provided by Commonsware. https://github.com/commonsguy/cwac-wakeful
要获得位置修正,我依赖于由成员叫做费多尔提供了以下code。 <一href="http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a">What是获得在Android的用户的当前位置,最简单,最可靠的方法?。我稍微修改了它为空,而不是返回得到最后的已知位置
To get location fixes, I rely on the following code provided by a member called Fedor. What is the simplest and most robust way to get the user's current location in Android?. I slightly modified it to return null instead of getting the last known location
他们都完全正常工作时,不结合:我可以从费多尔的一流位置锁定在一个活动,我可以做一些基本的东西(如登录的东西)使用Commonsware类
They both work perfectly fine when not combined: I can get a location fix from Fedor's class in an Activity, and I can do some basic stuff ( like logging something) using Commonsware class.
当我试图让从Commonsware的WakefulIntentService子的位置修复出现问题。该LocationListeners没有反应locationUpdates,我得到这些警告(我不明白还线程,处理程序的细微之处...)。有人可以帮助我了解的问题是什么?谢谢,祝大家新年快乐:)
The problem occurs when I am trying to get location fixes from the Commonsware's WakefulIntentService subclass. The LocationListeners do not react to locationUpdates and I get these warnings (I don't get yet the subtleties of threads, handlers, ...) . Can someone help me understand what the problem is? Thanks and I wish you all a very happy new year :)
12-31 14:09:33.664: W/MessageQueue(3264): Handler (android.location.LocationManager$ListenerTransport$1) {41403330} sending message to a Handler on a dead thread
12-31 14:09:33.664: W/MessageQueue(3264): java.lang.RuntimeException: Handler (android.location.LocationManager$ListenerTransport$1) {41403330} sending message to a Handler on a dead thread
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessage(Handler.java:383)
12-31 14:09:33.664: W/MessageQueue(3264): at android.location.LocationManager$ListenerTransport.onLocationChanged(LocationManager.java:193)
12-31 14:09:33.664: W/MessageQueue(3264): at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:58)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Binder.execTransact(Binder.java:338)
12-31 14:09:33.664: W/MessageQueue(3264): at dalvik.system.NativeStart.run(Native Method)
这是我WakefulIntentService子类:
This is my WakefulIntentService subclass:
public class AppService extends WakefulIntentService {
public static final String TAG = "AppService";
public AppService() {
super("AppService");
}
public LocationResult locationResult = new LocationResult() {
@Override
public void gotLocation(final Location location) {
if (location == null)
Log.d(TAG, "location could not be retrieved");
else
sendMessage(location);
}
};
@Override
protected void doWakefulWork(Intent intent) {
PhoneLocation myLocation;
myLocation = new PhoneLocation();
myLocation.getLocation(getApplicationContext(), locationResult);
}
在这里,费多尔的类的副本(略作修改:无需GPS也不是最后一个已知位置的)
And here a copy of Fedor's class (slightly modified: no need of GPS nor last known location)
public class PhoneLocation {
public static String TAG = "MyLocation";
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled=false;
boolean network_enabled=false;
public boolean getLocation(Context context, LocationResult result)
{
//I use LocationResult callback class to pass location value from MyLocation to user code.
locationResult=result;
if(lm==null) lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
//don't start listeners if no provider is enabled
if(!gps_enabled && !network_enabled)
return false;
if(network_enabled) lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
timer1=new Timer();
timer1.schedule(new ReturnNullLocation(), 20000);
return true;
}
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class ReturnNullLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerNetwork);
locationResult.gotLocation(null);
}
}
public static abstract class LocationResult{
public abstract void gotLocation(Location location);
}
}
推荐答案
您不能安全地从一个 IntentService
,因为 IntentService 注册监听器code>消失,只要
onHandleIntent()
(又名 doWakefulWork()
)完成。相反,你需要使用一个普通的服务,再加上处理像超时的详细信息(例如,用户在一个地下室,无法获得GPS信号)。
You cannot safely register listeners from an IntentService
, as the IntentService
goes away as soon as onHandleIntent()
(a.k.a., doWakefulWork()
) completes. Instead, you will need to use a regular service, plus handle details like timeouts (e.g., the user is in a basement and cannot get a GPS signal).
我有处理这一个清醒的方式的一个组成部分,但它并不像成熟的 WakefulIntentService
。
I have a component that handles this in a wakeful fashion, though it is not as mature as WakefulIntentService
.
这篇关于从IntentService得到一个位置上时,一个死线程发送消息到一个处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!