如何在每秒或每10秒后获取设备当前位置?

时间:2017-10-26 12:53:00

标签: android android-studio

当我尝试使用GPS追踪器获取当前位置时,它只是第一次增加时间,但我想在特定间隔后获取位置?我正在逐行获取所有地址,如地点子地区以及所有但是在第一次间隔之后,任何解决方案都将受到赞赏..

2 个答案:

答案 0 :(得分:5)

在后台启动您的应用服务,以及时获取位置信息。

public class MYService extends Service implements LocationListener {

}

并及时执行异步任务以获取定期位置更新。

TimerTask doAsynchronousTask = new TimerTask() {
    @Override
    public void run() {
        handler.post(new Runnable() {
            public void run() {

            }
        });
    }
};

//Starts after 20 sec and will repeat on every 20 sec of time interval.
timer.schedule(doAsynchronousTask, 20000,20000);  // 20 sec timer

它将每20秒提供一次位置更新。

答案 1 :(得分:2)

试试这段代码。 制作interface GetLocation

public interface GetLocation {
    public void onLocationChanged(Location location);
    public void onStatusChanged(String s, int i, Bundle bundle);
    public void onProviderEnabled(String s);
    public void onProviderDisabled(String s);
}

然后创建一个类 CurrentLocation implements LocationListener

public class CurrentLocation implements LocationListener {

    Context context;
    LocationManager locationManager;
    String provider;

    GetLocation getLocation;

    public CurrentLocation(Context context) {

        this.context = context;
        getLocation = (GetLocation) context;
        location();
    }

    public void location() {
        // Getting LocationManager object
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        // anruag getting last location
      //  Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        // Creating an empty criteria object
        Criteria criteria = new Criteria();

        // Getting the name of the provider that meets the criteria
        provider = locationManager.getBestProvider(criteria, false);

        if (provider != null && !provider.equals(" ")) {

            // Get the location from the given provider
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            Location location = locationManager.getLastKnownLocation(provider);

            locationManager.requestLocationUpdates(provider, 20000, 1, this);

            if (location != null)
                onLocationChanged(location);
            else {

            }
                // Toast.makeText(context, "Location can't be retrieved", Toast.LENGTH_SHORT).show();

        } else {
            Toast.makeText(context, "No Provider Found", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onLocationChanged(Location location) {

        // Log.e("Location", location.getProvider() + "==" + location.getAccuracy() + "==" + location.getAltitude() + "==" + location.getLatitude() + "==" + location.getLongitude());
        getLocation.onLocationChanged(location);
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude());

        ConstantValues.UPlat = String.valueOf(location.getLatitude());
        ConstantValues.UPlng = String.valueOf(location.getLongitude());

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        Log.e("onStatusChanged", "==" + s);
        getLocation.onStatusChanged(s, i, bundle);
    }

    @Override
    public void onProviderEnabled(String s) {
        Log.e("onProviderEnabled", "==" + s);
        getLocation.onProviderEnabled(s);
    }

    @Override
    public void onProviderDisabled(String s) {
        Log.e("onProviderDisabled", "==" + s);
        getLocation.onProviderDisabled(s);
        // alertbox("GPS STATUS", "Your GPS is: OFF");
        // Toast.makeText(context, "Please turn on the GPS to get current location.", Toast.LENGTH_SHORT).show();

        try {

            ConstantValues.showDialogOK("Please turn on the GPS to get current location.", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    switch (i) {
                        case DialogInterface.BUTTON_POSITIVE:
                            Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            context.startActivity(myIntent);
                            dialogInterface.dismiss();
                            break;
                        case DialogInterface.BUTTON_NEGATIVE:
                            dialogInterface.dismiss();
                            break;
                    }
                }
            }, context);
        } catch (Exception e) {
            Log.e("exception", e.toString()+"==");
        }

    }

}

在您想要获取当前位置的任何Activity中调用此类

CurrentLocation currentLocation;

最小距离变化时间间隔

声明这两个全局变量
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;
    // Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; 

onCreate

中设置其对象
currentLocation = new CurrentLocation(this);

制作方法

public void locationWithPermission() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (checkAndRequestPermissions()) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    MINIMUM_TIME_BETWEEN_UPDATES,
                    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new CurrentLocation(this));

        }
    }

并在Activity中针对您想要获取位置的任何事件调用此方法