android - 从 IntentService 获取位置时向死线程上的 Handler 发送消息

标签 android commonsware-cwac

我的应用需要定期进行位置修复,即使手机没有唤醒也是如此。 为此,我将 IntentService 与 Commonsware 慷慨提供的模式一起使用。 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 而不是获取最后一个已知位置

它们在不结合时都可以正常工作:我可以在 Activity 中从 Fedor 的类中获取位置修复,并且可以使用 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 类(class)的副本(稍作修改:不需要 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);

}

}

最佳答案

您不能安全地从 IntentService 注册监听器,因为一旦 onHandleIntent()(也称为 IntentService 就会消失doWakefulWork()) 完成。相反,您需要使用常规服务,并处理超时等详细信息(例如,用户在地下室,无法获得 GPS 信号)。

关于android - 从 IntentService 获取位置时向死线程上的 Handler 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8690198/

相关文章:

Android - 比较文本文件内容上的字符串输入

android - 相机预览在某些设备上旋转

android - 使 WakefulIntentService 等待位置回调被调用

android - 如何将复选框设置为字符串中的自定义文本并保存到数据库中?

java - WebView加载youtube页面

java - 相关实体的 GreenDao getter 返回空列表

android - Android 4.0.3中如何通过Socket进行通信

android - 装载机 : onLoadFinished called only once

android - 带有加载器和 EndlessCursorAdapter 的 ListFragment - 噩梦