【发布时间】:2013-06-06 15:16:53
【问题描述】:
我正在尝试构建一个应用程序,该应用程序具有每 X 分钟将当前位置发送到服务器的服务。
查看 SO 中的其他帖子,发现 commons-ware https://github.com/commonsguy/cwac-wakeful 中概述的方法效果最好。这里的 demo2 应用程序对我来说很好,它每 x 分钟向 Alarm.txt 写入一条消息。
我对“AppService.java”进行了以下修改,以便它尝试获取一个位置,而不是它作为示例程序所做的事情。
public class AppService extends WakefulIntentService {
private Context context;
public AppService() {
super("AppService");
}
@Override
protected void doWakefulWork(Intent intent) {
// File log=new File(Environment.getExternalStorageDirectory(),
// "AlarmLog.txt");
//
// try {
// BufferedWriter out=new BufferedWriter(new FileWriter(log.getAbsolutePath(),
// log.exists()));
//
// out.write(new Date().toString());
// out.write("\n");
// out.close();
// Log.e("AppService", "wrote to log file");
// }
// catch (IOException e) {
// Log.e("AppService", "Exception appending to log file", e);
// }
context = this.getApplicationContext();
Log.e("AppService", "Attaempting to get location");
new GetLocation(context).execute();
}
}
我的 GetLocation.java 看起来像这样
public class GetLocation {
private LocationManager locationManager;
private LocationListener locationListener;
private Context context;
public GetLocation(Context context) {
this.context = context;
}
public void execute() {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
new SendLocation2(location).execute();
locationManager.removeUpdates(locationListener);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
Log.e("AppService", "Registered to get location");
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
发送位置使用 apache http 客户端库将位置上传到服务器。
当我运行它时,我会在 logcat 中得到以下信息。
06-06 08:06:27.031: E/AppService(3161): Attaempting to get location
06-06 08:06:27.039: E/AppService(3161): Registered to get location
W/MessageQueue(3161): Handler (android.location.LocationManager$ListenerTransport$1) {41855e68} sending message to a Handler on a dead thread
W/MessageQueue(3161): java.lang.RuntimeException: Handler (android.location.LocationManager$ListenerTransport$1) {41855e68} sending message to a Handler on a dead thread
W/MessageQueue(3161): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
W/MessageQueue(3161): at android.os.Handler.enqueueMessage(Handler.java:618)
W/MessageQueue(3161): at android.os.Handler.sendMessageAtTime(Handler.java:587)
W/MessageQueue(3161): at android.os.Handler.sendMessageDelayed(Handler.java:558)
W/MessageQueue(3161): at android.os.Handler.sendMessage(Handler.java:495)
W/MessageQueue(3161): at android.location.LocationManager$ListenerTransport.onLocationChanged(LocationManager.java:218)
W/MessageQueue(3161): at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:58)
W/MessageQueue(3161): at android.os.Binder.execTransact(Binder.java:351)
W/MessageQueue(3161): at dalvik.system.NativeStart.run(Native Method)
我看起来在位置管理器调用它的回调 onLocationChanged() 之前,该应用程序不会保持活动状态。我该如何解决这个问题?
【问题讨论】:
标签: android locationmanager android-broadcast commonsware-cwac