当前位置: 代码迷 >> Android >> android 中GPS的使有有关问题
  详细解决方案

android 中GPS的使有有关问题

热度:99   发布时间:2016-05-01 22:02:15.0
android 中GPS的使有问题
Java code
public class GpsActivity extends Activity {    LocationManager lm;    TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.setContentView(R.layout.gps);        tv =(TextView) this.findViewById(R.id.tv1);        lm= (LocationManager)  getSystemService(Context.LOCATION_SERVICE);        List<String> proviers = lm.getAllProviders();        for (String string : proviers) {            System.out.println(string);        }        Location  location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);        updateLocation(location);                         }    private void updateLocation(Location location){        if(location!=null){            StringBuffer sb = new StringBuffer();            sb.append("当前位置信息\n");            sb.append("经度:").append(location.getLongitude())            .append("\n纬度:").append(location.getLatitude())            .append("\n高度:").append(location.getAltitude())            .append("\n方向:").append(location.getBearing());            tv.setText(sb.toString());            System.out.println(sb.toString());        }else{            tv.setText("没有获取到信息 ");        }    }}

权限我也加上了。为什么Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
老是为空。那里问了问题了。

------解决方案--------------------
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
把location声明全局变量应该就可以了
------解决方案--------------------
是不是你手机的gps相关设置没有开启,可以检查一下
  相关解决方案