我正在开发一个项目,通过android上的gps在10秒内获得地理定位。我正在使用后台服务,我正在使用requestLocationUpdates
来获取确切的位置,问题是10秒没有足够的时间进行GPS锁定它通常需要20秒到1分钟的精确锁定,以解决这个问题。问题我将时间间隔增加到3分钟,然后gps激活,锁定卫星并获得精确的地理位置。但是在获得准确位置GPS进入睡眠状态并且GPS图标从屏幕消失之后,直到下一次更新。课程中有stopUsingGPS()
,但我没有在我的代码中的任何地方打电话或使用。正如你们知道当你打开yandex navi或谷歌地图时,它会保持GPS打开并锁定,直到你销毁应用程序,我怎么能这样做?我尝试过这种方法Android: How to keep GPS active until more accurate location is provided?但结果是一样的,GPS图标出现,锁定到卫星,获得地理位置并进入睡眠状态,我如何保持GPS打开并锁定直到我停止服务?
提前致谢。
这是我的GPSTracker类;
package com.atalay.reporter.Control;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import com.atalay.reporter.Service.Reporter;
/**
* Created by BARISUSER on 9.12.2015.
*/
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
double speed;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context mContext) {
this.mContext = mContext;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(mContext.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
{
//B.A. 08.12.2015 TODO: Buradan devam et
showSettingsAlert();
} else
{
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled)
{
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null)
{
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
speed = location.getSpeed();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
speed = location.getSpeed();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getSpeed ()
{
if(location != null)
{
speed = location.getSpeed();
}
else
{
speed = 0;
}
return speed;
}
public double getLatitude(){
if(location != null)
{
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
Reporter.aktifEnlem = location.getLatitude();
Reporter.aktifBoylam = location.getLongitude();
Reporter.hiz = location.getSpeed();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
我的记者班;
package com.atalay.reporter.Service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import com.atalay.reporter.Common.ApiHelper;
import com.atalay.reporter.Common.Utils;
import com.atalay.reporter.Control.GPSTracker;
/**
* Created by BARISUSER on 7.12.2015.
*/
public class Reporter extends Service {
final static long Dakika = 10000;
private ApiHelper apiHelper;
private GPSTracker gpsTracker;
public static double aktifEnlem = 0;
public static double aktifBoylam = 0;
public static double hiz = 0;
private boolean ilkKonum = false;
@Override
public void onCreate(){
super.onCreate();
apiHelper = new ApiHelper();
gpsTracker = new GPSTracker(Reporter.this);
if(!ilkKonum){
if(aktifBoylam==0 || aktifEnlem==0)
if(gpsTracker.canGetLocation()){
aktifEnlem = gpsTracker.getLatitude();
aktifBoylam = gpsTracker.getLongitude();
hiz = gpsTracker.getSpeed();
}
}
}
@Override
public void onDestroy()
{
//gpsTracker.stopUsingGPS();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
new Thread(new Runnable() {
@Override
public void run() {
while (true)
{
try {
if (Utils.InternetControl(Reporter.this.getApplicationContext())) {
isYap();
}
Thread.sleep(Dakika * 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return Service.START_STICKY;
}
public String getIMEI(Context context)
{
TelephonyManager mngr = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
String imei = mngr.getDeviceId();
return imei;
}
private void isYap()
{
if(aktifBoylam!=0 || aktifEnlem!=0)
{
apiHelper.setParam("enlem", aktifEnlem);
apiHelper.setParam("boylam", aktifBoylam);
apiHelper.setParam("hiz", Math.round(hiz/1000));//hiz means speed
apiHelper.setParam("cid", getIMEI(this));
apiHelper.responseString(ApiHelper.ResponseType.POST);
}
}
}