重新启用GPS后,Android更新GPS位置

时间:2018-06-29 08:59:41

标签: android gps

我发现了这个 how I can get the city name of my current position?并构建显示的代码。如果在活动开始时启用了gps,则此方法效果很好,但是如果您禁用并再次启用gps,则它不会显示任何位置。

package com.barcodegame;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class Achivement extends Activity implements LocationListener {
    private TextView latituteField;
    private TextView longitudeField;
    private TextView addressField;
    private TextView reinfeld;
    private TextView luebeck;
    private LocationManager locationManager;
    private String provider;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.achievement);
        latituteField = (TextView) findViewById(R.id.textViewLat);
        longitudeField = (TextView) findViewById(R.id.textViewLong);
        addressField = (TextView) findViewById(R.id.textViewCity);
        reinfeld = findViewById(R.id.textViewReinfeld);
        luebeck = findViewById(R.id.textViewLuebeck) ;

        // New LocationManager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        // Get best Provider
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);

        // Test if permission is granted
        if (ActivityCompat.checkSelfPermission(
                this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(Achivement.this,new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION},123);
        } else {
            Location location = locationManager.getLastKnownLocation(provider);


            if (location != null) {
                Log.d("marcel", "Provider " + provider + " has been selected.");
                onLocationChanged(location);
            } else {
                latituteField.setText("Location not available");
                longitudeField.setText("Location not available");
            }
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (ActivityCompat.checkSelfPermission
                (this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(Achivement.this,new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION},123);
            return;
        }
        else {locationManager.requestLocationUpdates(
                provider, 400, 1, this); }
    }


    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();

        Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> address = geoCoder.getFromLocation(lat, lng, 1);

            // Debug only
            latituteField.setText(String.valueOf(lat));
            longitudeField.setText(String.valueOf(lng));
            // if location is reinfeld set reinfeld
            if (address.get(0).getLocality().equals("Reinfeld")) {
                reinfeld.setText("Visit Reinfeld ✔");
            }
            if (address.get(0).getLocality().equals("Lübeck")) {
                luebeck.setText("Visit Lübeck ✔");
            }
            // Debug only
            addressField.setText(address.get(0).getLocality()); //This will display the final address.

        } catch (IOException e) {
            // Handle IOException
        } catch (NullPointerException e) {
            // Handle NullPointerException
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {


    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }
}

我找不到问题,也不知道这是否真的是问题,或者我的设备在重新启动gps之后只是没有得到gps。

有没有办法强制gps更新?

0 个答案:

没有答案