漆黑天使复活 2016-05-14 11:26 采纳率: 44.4%
浏览 1698
已采纳

GPS_PROVIDER无法获取定位

代码如下

 public class MainActivity extends AppCompatActivity
{

    LocationManager locManager;

    EditText show;

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

        show = (EditText) findViewById(R.id.EditText);

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

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    123);}
        locManager = (LocationManager) getSystemService
                (Context.LOCATION_SERVICE);



        Location location = locManager.getLastKnownLocation(
                LocationManager.GPS_PROVIDER);

        updateView(location);

        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER
                , 3000, 8, new LocationListener()  // ①
                {
                    @Override
                    public void onLocationChanged(Location location)
                    {

                        updateView(location);
                        Log.v("MSG","1");
                    }
                    @Override
                    public void onProviderDisabled(String provider)
                    {
                        updateView(null);
                        Log.v("MSG","2");
                    }
                    @Override
                    public void onProviderEnabled(String provider)
                    {


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

                            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                    123);}
                        updateView(locManager
                                .getLastKnownLocation(provider));
                        Log.v("MSG","3");
                    }
                    @Override
                    public void onStatusChanged(String provider, int status,
                                                Bundle extras)
                    {
                    }
                });
    }

    public void updateView(Location newLocation)
    {
        if (newLocation != null)
        {
            StringBuilder sb = new StringBuilder();
            sb.append("实时的位置信息:\n");
            sb.append("经度:");
            sb.append(newLocation.getLongitude());
            sb.append("\n纬度:");
            sb.append(newLocation.getLatitude());
            sb.append("\n高度:");
            sb.append(newLocation.getAltitude());
            sb.append("\n速度:");
            sb.append(newLocation.getSpeed());
            sb.append("\n方向:");
            sb.append(newLocation.getBearing());
            show.setText(sb.toString());
        }
        else
        {

            show.setText("无法获取地理信息");
            Log.v("MSG","4");
        }
    }
}

权限都已申请,还根据6.0的新变化,增加了运行时权限申请,但是还是无法获得定位信息,
把GPS_PROVIDER换成NETWORK_PROVIDER就可以获得定位,咋回事?

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-10-07 17:10
    关注

    ER无法获取定位 30C
    代码如下
    public class MainActivity extends AppCompatActivity
    {

    LocationManager locManager;
    
    EditText show;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        show = (EditText) findViewById(R.id.EditText);
    

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

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    123);}
        locManager = (LocationManager) getSystemService
                (Context.LOCATION_SERVICE);
    
    
    
        Location location = locManager.getLastKnownLocation(
                LocationManager.GPS_PROVIDER);
    
        updateView(location);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 8月23日

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波