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

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);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题