我的应用程序需要定期修复定位,即使手机未醒来也是如此。 为此,我使用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班级的副本(略有修改:不需要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);
}
}
答案 0 :(得分:34)
您无法安全地注册IntentService
的听众,因为IntentService
(a.k.a。,onHandleIntent()
)完成后doWakefulWork()
就会消失。相反,您需要使用常规服务,以及超时等处理细节(例如,用户位于地下室,无法获取GPS信号)。
答案 1 :(得分:12)
我遇到了类似的情况,我使用了android Looper工具效果很好: http://developer.android.com/reference/android/os/Looper.html
具体地说,在调用设置监听器的函数之后,我调用:
Looper.loop();
阻止当前线程,因此它不会死,但同时让它处理事件,因此您将有机会在调用侦听器时收到通知。一个简单的循环会阻止您在调用侦听器时收到通知。
当监听器被调用并且我已经完成处理它的数据时,我把:
Looper.myLooper().quit();
答案 2 :(得分: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"
>
)