将Gps作为后台服务运行,并将接收到的当前位置数据与Sqlite中的值进行比较? [英] Run Gps as background service and Compare the received current Location Data with value in Sqlite?

查看:115
本文介绍了将Gps作为后台服务运行,并将接收到的当前位置数据与Sqlite中的值进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于这个的问题,我已经经历了几天,但是找不到合理的答案.我是android的新手,所以我不知道如何完成工作

I know there are many questions that have been asked regarding this and i have been going through all that from couple of days but couldn't find a reasonable answer.I am newbie to android so i have no idea how things get done.

基本上,我想知道如何在后台运行GPS即服务,例如在安装应用程序时启动它,以及如何将从gps后台服务接收到的数据与SQLite数据库中的数据进行比较.

Basically I want to know how to run GPS as a service in background like when the application is installed it starts and how to compare that data received from gps background service with data in SQLite Data Base.

推荐答案

您可以像上面那样进行服务,该服务从GPS和网络提供商处获取位置,并在后台插入数据库中.

you can make service like above , this service get location from GPS and network provider and insert into database in background.

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数据库,因此您必须从主要活动"之类的地方启动该服务

this service insert location to SQL lite database and you must start this service from somewhere like Main Activity

        try{
        startService(new Intent(getBaseContext(), LocationTracker.class));
    }catch (Exception ex){
    }

并与SQL lite位置进行比较,这种有趣的计算点之间的距离

and to compare with SQL lite location, this fun calculate distance between to point

    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中的值进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆