35

我的应用程序需要定期进行位置修复,即使手机没有唤醒也是如此。为此,我将 IntentService 与 Commonsware 慷慨提供的模式一起使用。 https://github.com/commonsguy/cwac-wakeful

为了获得位置修复,我依赖于一个名为 Fedor 的成员提供的以下代码。 在 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 课程的副本(稍作修改:不需要 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);

}

}

4

3 回答 3

35

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

于 2011-12-31T20:09:00.837 回答
12

我也遇到过类似的情况,我使用了 android Looper 工具效果很好:http: //developer.android.com/reference/android/os/Looper.html

具体来说,在调用设置监听器的函数之后,我调用:

Looper.loop();

这会阻塞当前线程,因此它不会死亡,但同时让它处理事件,因此您将有机会在调用侦听器时收到通知。一个简单的循环会阻止您在调用侦听器时收到通知。

当监听器被调用并且我完成处理它的数据时,我输入:

Looper.myLooper().quit();
于 2012-10-23T13:48:32.260 回答
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"
    >

)

于 2012-04-25T10:04:06.077 回答