android - 设备锁定时发送位置更新

标签 android location android-service

在我的特定屏幕上的应用程序中,即使屏幕已锁定,我也会向服务器发送位置更新。

下面是用于跟踪位置更新的 GPSTracker 服务类。

public class GPSTracker extends Service implements LocationListener {

    private final Context mContext;
    public static CommonInterfaces.UpdateLocationDelegate delegate;

    // 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

    private static final  int MY_PERMISSION_ACCESS_COURSE_LOCATION = 0;

    // The minimum distance to change Updates in meters
    public static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 20 meters

    public static final long MAX_DISTANCE_CHANGE_FOR_UPDATES = 45; // considering 100 miles/hr

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000; // location update is based on distance, thus the value here is 0 ;//1000 * 60 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public GPSTracker(){
        mContext = null;
    }

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        if (ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity)mContext, new String[]{
                    android.Manifest.permission.ACCESS_FINE_LOCATION
            }, 10);
        }

        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // Getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // Getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // No network provider is enabled
            } else {
                this.canGetLocation = true;

                // If GPS enabled, get latitude/longitude using GPS Services
                if (isGPSEnabled)  {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
//                        Toast.makeText(getApplicationContext(), "GPS Location", Toast.LENGTH_SHORT).show();
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }

                if (isNetworkEnabled && (location == null)) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//                        Toast.makeText(getApplicationContext(), "Network Location", Toast.LENGTH_SHORT).show();
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }
        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 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/Wi-Fi enabled
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }


    /**
     * Function to show settings alert dialog.
     * On pressing the Settings button it will launch 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 the 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 the 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) {
        if (delegate != null) {
            delegate.onUpdateLocation(location);
        }
    }


    @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;
    }
}

onLocationChanged 方法检测位置变化并调用委托(delegate)方法。但当设备锁定超过 5 分钟时,此服务不会更新位置。我猜 android 将进入休眠模式,并在后台杀死应用程序。无论如何,我可以在设备锁定时发送位置更新并避免进入休眠模式吗?

最佳答案

我在这个领域做了很多工作(“永远”获得频繁的位置更新)。如果您每小时需要多次位置更新,唯一的方法是让您的服务处于前台(使用 START_STICKY - 请参见示例)。如果该服务保留在前台(即使在 Doze Mode 中),它几乎可以“永远”存在。在打瞌睡模式下,位置更新可能会停止,因为某些传感器“休眠”,但在打瞌睡模式下服务不得受到影响。

可以引用下google示例:Location Updates in Foreground Sample

注意: 请记住,如果前台服务也需要(内存不足等),Android 也可以将其终止,但前台将是 Android 关闭的最后一件事。

关于android - 设备锁定时发送位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49329197/

相关文章:

android - INSTALL_FAILED_NO_MATCHING_ABIS : Failed to extract native libraries, res=-113 设备

android - 在Gradle Sync上自动重建Android Studio项目

android - 为什么 getSpeed() 在 android 上总是返回 0

java - Android手持设备和Wear通信不一致

java - Android Wear Watch Face获取手机电量百分比

android - 如何限制 spannablestring 的行高但保持行的其余部分

android - 未检索到纬度和经度

街道上的android限速

android - 从服务获取位置数据?

android - 当我在 Kotlin 中使用 Anko 时,如何为 SQLite 的表定义一个非空字段?