将 Gps 作为后台服务运行并将接收到的当前位置数据与 Sqlite 中的值进行比较?
Posted
技术标签:
【中文标题】将 Gps 作为后台服务运行并将接收到的当前位置数据与 Sqlite 中的值进行比较?【英文标题】:Run Gps as background service and Compare the received current Location Data with value in Sqlite? 【发布时间】:2017-01-02 01:24:59 【问题描述】:我知道有很多关于这个问题的问题,我已经经历了几天但找不到合理的答案。我是 android 的新手,所以我不知道事情是如何完成的.
基本上,我想知道如何在后台运行 GPS 作为服务,例如在安装应用程序时启动它,以及如何比较从 gps 后台服务接收到的数据与 SQLite 数据库中的数据。
【问题讨论】:
关于run in background on install
***.com/questions/2127044/… 的问题的第一部分您可能希望将问题分解为更小的步骤并展示您尝试过的内容。
【参考方案1】:
您可以像上面一样进行服务,该服务从 GPS 和网络提供商获取位置并在后台插入数据库。
package com.example.alireza.locationtracker;
import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.Hashtable;
import java.util.Map;
public class LocationTracker extends Service
public static LocationListener gpsloclistener;
public static LocationListener netlistener;
public static LocationManager mLocationManager;
public static Location gpsLOc;
public static Location netLOc;
private static Location gpslocation;
private static Location netlocation;
public static boolean isGPSEnabled;
public static boolean isNetworkEnabled;
@Override
public void onCreate()
super.onCreate();
gpsloclistener = new gpsloclistener();
netlistener = new netlistener();
getLocation();
// myfunc();
@Override
public IBinder onBind(Intent intent)
return null;
public void getLocation()
try
mLocationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled || isNetworkEnabled)
if (isGPSEnabled)
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,180000,0,gpsloclistener);
if (isNetworkEnabled)
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, netlistener);//aslan bar roye network nemishe mahdodiyat gozasht
catch (Exception e)
e.printStackTrace();
public class gpsloclistener implements LocationListener
@Override
public void onLocationChanged(final Location location)
if (location.getAccuracy() < 20)
G.database.execSQL("INSERT INTO Gps_Location (Gps_lat,Gps_long,Gps_speed,Gps_time,Gps_bearing,Gps_alt,Gps_acur) VALUES ('" + location.getLatitude() + "','" + location.getLongitude() + "','" + location.getSpeed() + "','" + System.currentTimeMillis() + "','" + location.getBearing() + "','" + location.getAltitude() + "','" + location.getAccuracy() + "')");
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
@Override
public void onProviderEnabled(String provider)
getLocation();
@Override
public void onProviderDisabled(String provider)
private class netlistener implements LocationListener
@Override
public void onLocationChanged(final Location location)
G.database.execSQL("INSERT INTO Net_Location (Net_lat,Net_long,Net_speed,Net_time,Net_bearing,Net_alt,Net_acur) VALUES ('" + location.getLatitude() + "','" + location.getLongitude() + "','" + location.getSpeed() + "','" + System.currentTimeMillis() + "','" + location.getBearing() + "','" + location.getAltitude() + "','" + location.getAccuracy() + "')");
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
@Override
public void onProviderEnabled(String provider)
getLocation();
@Override
public void onProviderDisabled(String provider)
此服务将位置插入 SQL lite 数据库,您必须从 Main Activity 之类的某个位置启动此服务
try
startService(new Intent(getBaseContext(), LocationTracker.class));
catch (Exception ex)
并与 SQL lite 位置进行比较,这个有趣的计算点之间的距离
public static double distFrom(double lat1, double lng1, double lat2, double lng2)
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;
int meterConversion = 1609;
double v = dist * meterConversion;
return v;
最后定义服务清单
<service android:name=".LocationTracker"/>
【讨论】:
以上是关于将 Gps 作为后台服务运行并将接收到的当前位置数据与 Sqlite 中的值进行比较?的主要内容,如果未能解决你的问题,请参考以下文章