Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 设备锁定时发送位置更新_Android_Location_Android Service - Fatal编程技术网

Android 设备锁定时发送位置更新

Android 设备锁定时发送位置更新,android,location,android-service,Android,Location,Android Service,在特定屏幕上的应用程序中,即使屏幕锁定,我也会向服务器发送位置更新 下面是用于跟踪位置更新的GPSTracker服务类 public class GPSTracker extends Service implements LocationListener { private final Context mContext; public static CommonInterfaces.UpdateLocationDelegate delegate; // flag fo

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

下面是用于跟踪位置更新的
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
方法检测到位置更改并调用委托方法。但当设备锁定超过5分钟时,此服务不会更新位置。我猜android将进入睡眠模式,并在后台关闭应用程序。在设备被锁定时,我是否可以发送位置更新并避免进入睡眠模式?

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

您可以参考下一个谷歌示例:


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

对于前景服务,设备不应该一直处于唤醒状态吗?前景服务将在打瞌睡模式下执行(设备深度睡眠)。检查我编辑的答案以检查谷歌示例。不是您当前的问题,但-不要使用此代码。此类已完全中断,大部分时间无法工作。请参阅我的writeup和alternative,网址为