简体   繁体   中英

GPS location working only on NETWORK_PROVIDER?

I'm using Android Studio 3.0.1 and trying to get GPS location using API 19 (KitKat), and it's an activity (AppCompatActivity) which extends FragmentActivity.
I have the GPS activated, and i'm using a real device "Alcatel One Touch" which i did update its services (google services, maps, ...etc), battery 100%.
And i have my permission setup : <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

Here's my code :

LocationManager locationManager;
LocationListener listener;

public void start()
{
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    listener = new LocationListener()
    {
        @Override
        public void onLocationChanged(Location location)
        {
            String ss = String.valueOf(location.getLatitude()) + " || " + String.valueOf(location.getLongitude() + " : " + location.getProvider());
            Log.i("Location", ss);
            Toast.makeText(MainActivity.this, ss, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle)
        {
            Toast.makeText(MainActivity.this, "changed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderEnabled(String s)
        {
            Toast.makeText(MainActivity.this, "enabled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderDisabled(String s)
        {
            Toast.makeText(MainActivity.this, "disabled", Toast.LENGTH_SHORT).show();
        }
    };


    if(Build.VERSION.SDK_INT < 23 )
    {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); // my case
    }
    else
    {
        int Check_Permission = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION);
        int Permission_Granted = PackageManager.PERMISSION_GRANTED;
        if (Check_Permission != Permission_Granted)
        {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},10);
        }
        else
        {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
        }
    }       
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    Toast.makeText(this, "Granted", Toast.LENGTH_SHORT).show();

    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
    {
        if(ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
        }
    }
}  

onLocationChanged is never called, unless i change

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

to

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);

It worked immediately, but why it's not working on GPS_PROVIDER ? and on GPS_PROVIDER i waited like 30 minutes just in case the GPS would take too long to get the location, but it didn't.

好的,事实证明,即使我移动了,我也必须走得很远才能使GPS定位并获得更好的信号,我还是使用GPS测试来跟踪信号功率,并且按照应用程序中的说明重新启动了手机。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM