从IntentService获取位置信息时,向已死线程上的Handler发送消息。

35
我的应用程序需要定期进行位置修复,即使手机没有唤醒也要这样做。 为此,我使用了Commonsware慷慨提供的IntentService模式。 https://github.com/commonsguy/cwac-wakeful 为了获得位置修复,我依赖于一位名叫Fedor的成员提供的以下代码。 What is the simplest and most robust way to get the user's current location on Android?。我稍微修改了它,以返回null而不是获取最后已知的位置。
它们两个在单独使用时都非常好用:我可以从Fedor的类中获取Activity中的位置修复,并且我可以使用Commonsware类执行一些基本操作(例如记录某些内容)。
问题出现在当我尝试从Commonsware的WakefulIntentService子类获取位置修复时。LocationListeners无法对locationUpdates做出反应,我收到这些警告(我还不理解线程、处理程序等细节...)。有人能帮我理解问题是什么吗?谢谢,祝大家新年快乐 :)
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子类:
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);
}

这里是Fedor类的一个副本(稍作修改:不需要GPS或最后已知位置)

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);

}

}

3个回答

35

IntentService中不能安全地注册监听器,因为IntentService会在onHandleIntent()(也称为doWakefulWork())完成后立即关闭。相反,您需要使用普通服务,并处理细节,例如超时(例如,用户在地下室中无法获得GPS信号)。


1
它像魔法一样运行!非常感谢!我已经在我的应用程序子类中设置了闹钟,并创建了startLocationPoll()和stopLocationPoll()方法。新年快乐! - znat
当我使用链接上的示例时,我收到了"E/LocationPoller( 442): Invalid Intent -- has no provider"的错误信息,请问有什么线索吗? - user836026
1
@user836026: 您未能在 Intent 上提供 LocationPoller.EXTRA_PROVIDER 额外信息,指示要使用哪个位置提供程序。如果您对此组件有其他问题,请使用“提问”按钮,而不是在此处发布评论。 - CommonsWare
@CommonsWare: 谢谢... "无效的 Intent -- 没有提供" 的消息消失了,但是我尝试将 LocationReceiver 设置为 android.intent.action.BOOT_COMPLETED 的 intent-filter,但是它不起作用。我该如何让 LocationReceiver 在开机时工作? - user836026
@CodeShadow:那个库早已停用。我已从答案中删除它。 - CommonsWare
显示剩余5条评论

12

我曾经遇到过类似的情况,而我使用了Android的Looper工具来取得良好的效果:http://developer.android.com/reference/android/os/Looper.html

具体来说,在调用设置侦听器函数后,我立即调用:

Looper.loop();

它会阻塞当前线程,以便不死掉但同时让其处理事件,这样你就有机会在监听器被调用时收到通知。简单的循环会防止你在监听器被调用时获得通知。

当监听器被调用并且我完成其数据的处理后,我放置:

Looper.myLooper().quit();

1
我做同样的事情。但在 Looper.loop() 之前,我会添加 if(Looper.myLooper() == null) Looper.prepare(); - Ahmet Noyan Kızıltan
我还会运行一个单独的线程,在一定时间后超时(网络为5-15秒,GPS为45-120秒)。否则,Looper.loop()将会让设备一直保持唤醒状态。 - Ahmet Noyan Kızıltan

6

对于像我一样的人:我遇到了与AsyncTask有关的类似问题。据我所知,该类假定在UI(主)线程上初始化。

一个解决方法(可能有多个)是将以下内容放置在MyApplication类中:

@Override
public void onCreate() {
    // workaround for http://code.google.com/p/android/issues/detail?id=20915
    try {
        Class.forName("android.os.AsyncTask");
    } catch (ClassNotFoundException e) {}
    super.onCreate();
}

不要忘记在 AndroidManifest.xml 中提及它:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name="MyApplication"
    >

)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接