【问题标题】:Android - Log fused location updates to a file in a background serviceAndroid - 将融合位置更新记录到后台服务中的文件中
【发布时间】:2016-09-22 06:41:30
【问题描述】:

我正在尝试将融合位置更新记录到文件中,该更新来自在它自己的进程“:locationProcess”中运行的未绑定服务。

AndroidManifest.xml

<service
   android:name=".LocationUpdate"
   android:process=":locationProcess"
   android:enabled="true"
   android:exported="true" />

LocationUpdate.java

public class LocationUpdate extends Service implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {...

onBind 设置为返回 null

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

并且 onStartCommand 返回 START_STICKY。

我正在 Google play 服务中的 LocationUpdate 服务的 onConnected 回调方法中创建文件...

@Override
public void onConnected(Bundle arg0) {

    try {
        pwFile = new PrintStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "gps.csv"));
        pwFile.println("date,latitude,longtitude,altitude,bearing,accuracy");
    } catch (IOException e) {
        // TODO: Handle the exception
    }

    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    startLocationUpates();
}

并在 LocationListeneronLocationChange 回调中写出 GPS 详细信息...

@Override
public void onLocationChanged(Location location) {
    currentLat = location.getLatitude();
    currentLng = location.getLongitude();

    // Get current date time
    Date newDate = new Date(System.currentTimeMillis());

    // Write lat and long out to a file
    pwFile.println(newDate.toString() + "," + String.format("%.6f", currentLat) + "," +
            String.format("%.6f", currentLng) + "," + String.format("%.4f", location.getAltitude()) + "," +
            String.format("%.4f", location.getBearing()) + "," + String.format("%.4f", location.getAccuracy()));
}

我正在我的 Application 类的 onCreate 覆盖上启动服务...

public class App extends Application {
   Intent LocationService;

   @Override
   public void onCreate() {
       super.onCreate();

       LocationService = new Intent(this, LocationUpdate.class);
       startService(LocationService);
   }
}

这一切都有效,App 在 Manifest.xml 的 &lt;application&gt; 标签 android:name="mypackage.app" 中声明。

这部分不...

即使前台有另一个应用程序,我希望服务继续将位置记录到文件中。

如果用户故意关闭应用程序,它应该停止记录。阅读了 Android 文档后,我认为我的代码可以正常工作。

据我所知,当应用程序不在前台时,服务确实会继续运行。如果用户专门关闭了我不想要的应用程序,它会继续运行,并且由于某种原因我不明白服务本身在应用程序运行时被销毁和重新创建,所以 onConnected 在服务再次被调用,因此日志文件被有效覆盖。

我猜这更像是一个架构问题。

基本上,我希望我的位置服务在应用程序启动后不在前台时可靠地记录到文件中,并在应用程序被用户终止时停止。

希望是有道理的。我在 Github 上查看了很多示例,但它们似乎只在应用程序处于前台时才会记录。

非常感谢任何帮助,这让我有点发疯。

谢谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    您将服务设置为启动服务(而不是意图服务)。然后在服务的onStartCommand上,返回START_STICKY。这将告诉操作系统,如果出于任何原因需要关闭服务,请在有足够资源时再次运行它。在您的情况下,当用户关闭活动/终止应用程序时,服务进程无论如何都会被终止,但通常会重新打开。

    【讨论】:

      猜你喜欢
      • 2019-10-26
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多