3

我正在尝试构建一个应用程序,该应用程序具有每 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() 之前不会保持活动状态。我怎样才能解决这个问题?

4

1 回答 1

1

查看 SO 中的其他帖子,发现 commons-ware https://github.com/commonsguy/cwac-wakeful中概述的方法效果最好。

绝对不。任何形式的AnIntentService都不适合您的用例。AnIntentService不能注册侦听器、fork 自己的线程或做任何将继续运行超过onHandleIntent(). 一旦onHandleIntent()返回,如果没有进一步的命令要处理,服务将关闭,这反过来可能导致您的进程被终止。

对于需要注册侦听器的情况,请使用Service带有您自己的 managed 的​​常规 ,您可以WakeLock其中控制服务何时关闭。

于 2013-06-06T15:37:19.453 回答