运行 Gps 作为后台服务并将接收到的当前位置数据与 Sqlite 中的值进行比较?
Run Gps as background service and Compare the received current Location Data with value in Sqlite?
我知道有很多关于此的问题,我已经研究了几天,但找不到合理的 answer.I 我是 android 的新手所以我不知道事情是如何完成的。
基本上我想知道如何 运行 GPS 作为后台服务,例如当应用程序安装后启动,以及如何将从 gps 后台服务接收的数据与 SQLite 数据库中的数据进行比较。
您可以像上面那样提供服务,该服务从 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 精简版数据库,您必须从 Main Activity
等地方启动此服务
try{
startService(new Intent(getBaseContext(), LocationTracker.class));
}catch (Exception ex){
}
并与 SQL 精简位置进行比较,计算点之间的距离很有趣
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"/>
我知道有很多关于此的问题,我已经研究了几天,但找不到合理的 answer.I 我是 android 的新手所以我不知道事情是如何完成的。
基本上我想知道如何 运行 GPS 作为后台服务,例如当应用程序安装后启动,以及如何将从 gps 后台服务接收的数据与 SQLite 数据库中的数据进行比较。
您可以像上面那样提供服务,该服务从 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 精简版数据库,您必须从 Main Activity
等地方启动此服务 try{
startService(new Intent(getBaseContext(), LocationTracker.class));
}catch (Exception ex){
}
并与 SQL 精简位置进行比较,计算点之间的距离很有趣
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"/>