Android Geolocation在模拟器中工作但不在手机中工作

问题描述 投票:1回答:3

我创建了一个带有地理位置的简单应用程序,它在文本视图中显示用户的当前位置,如纬度,经度和Geocoder cityname countryname邮政编码等。

一切都在模拟器中完美运行,但由于某种原因,我的手机中无法检索到该位置。

模拟器正在运行Android 7.1,而我的手机正在运行Android 7.0,但这不应该是一个问题,因为我考虑到6.0 marshmallow的应用程序。

这是代码

“Maina c Chiゔぃty”

package com.example.slimshady.LocationInfoApp;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    LocationManager locationManager;
    LocationListener locationListener;
    Geocoder geocoder;
    TextView latitude, longitude, city, postcode, country;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        latitude = (TextView)findViewById(R.id.latitude);
        longitude = (TextView) findViewById(R.id.longitude);
        city = (TextView)findViewById(R.id.city);
        postcode = (TextView)findViewById(R.id.postcode);
        country = (TextView)findViewById(R.id.country);


        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                updateLocationInfo(location);

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };

        // asking permission starts here
        if (Build.VERSION.SDK_INT < 23){

            // api level lower than 23 so no need to ask for permission

            try {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locationListener);

            }
            catch (SecurityException e){

                e.printStackTrace();
            }
        }
        else {

            // api level greater than or equal to 23 checking if has permission if not VVVV this condition
            // means go ask for permission

            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){

                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
            }
            else{

                // means api level greater than or equal to 23 and already has permission
                // so no need to go out and ask for permission

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

                // last known location because we went to this part means we have been here before
                Location lastKnownLocation =  locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (lastKnownLocation != null) {
                    updateLocationInfo(lastKnownLocation);
                }



            }

        }
        // asking permission ends here


    }

    public void updateLocationInfo(Location location) {

        geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());

        try {

            List<Address> locationAddress = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(),1);
            if (locationAddress != null && locationAddress.size() > 0) {

                latitude.setText("Latitude: "+String.valueOf(locationAddress.get(0).getLatitude()));
                longitude.setText("Longitude: "+String.valueOf(locationAddress.get(0).getLongitude()));
                city.setText("City: "+String.valueOf(locationAddress.get(0).getLocality()));
                postcode.setText("Post Code: "+String.valueOf(locationAddress.get(0).getPostalCode()));
                country.setText("Country: "+String.valueOf(locationAddress.get(0).getCountryName()));

            }


        }
        catch (Exception e){

        }

    }

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

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

            startListening();

        }
    }

    public void startListening(){

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){

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

        }

    }
}

代码简单而基本,一切都应该工作正常,但由于某种原因它不会。

我的意思是我的物理设备中的GPS接收器与谷歌地图配合使用,所以我知道它没有坏掉。

看它在模拟器精细Image of it working in the emulator工作

但是相同的代码在我的手机上不起作用,我添加了所有权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>

看到同一个应用程序在我的物理设备上不起作用,这是我物理设备的截图

Screenshot of it not working on mobile

当您看到该位置已开启且我在弹出窗口中询问时已授予权限。最后但并非最不重要的是,显示GPS接收器工作并且在我的物理设备中没有损坏。

我使用谷歌地图的屏幕截图Screenshot of me using google maps我在红色圆圈上GPS,那些地图可以获得我的GPS位置

java android gps android-permissions android-gps
3个回答
1
投票

您的设备可以通过多种方式进行定位。当它们不是时,它们经常被混淆为“GPS”。

  1. GPS(全球定位系统)这是一种全球导航卫星系统(GNSS),它依靠来自轨道卫星星座的信号来定位设备。 GPS专门指美国国防部部署的卫星星座。替代品包括GLONASS(俄罗斯)和Galileo(欧洲)。
  2. 网络使用WiFi或蓝牙信号来定位您的设备。设备扫描发送器ID,然后通过其Internet连接将这些ID发送到服务器以在数据库中查找。服务器以位置响应。

您的代码目前仅使用LocationManager.GPS_PROVIDER,它仅使用卫星定位您的设备。 Google地图可能正在使用网络位置,该功能在禁用GPS时仍然有效。

我怀疑你需要:

  1. 添加LocationManager.NETWORK_PROVIDER
  2. 在您的设备上启用GPS。这是在“设置和位置”下的某个位置。

enter image description here


0
投票

我解决了像你的问题完全删除了位置管理器,并使所有位置工作基于FusedLocationProviderClient,我也有一些奇怪的错误在三星s8与LocationSettingsRequest,可以启动默认对话框打开gps,它是与错误,但删除locationManager.requestLocationUpdates后它融合了完美的效果。


0
投票

如果您在物理设备中使用已发布的apk,则必须在Google API控制台的限制部分中为发布apk添加SHA1。我认为这有帮助。

如果没有,那么请通过在物理设备中调试app来显示您的logcat错误。

© www.soinside.com 2019 - 2024. All rights reserved.