android GPS定位 及空值报错测试及分析

整个代码如下:

 private void toggleGPS(Context context) {
        Intent intent = new Intent();
        intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        intent.addCategory("android.intent.category.ALTERNATIVE");
        intent.setData(Uri.parse("custom:3"));
        context.sendBroadcast(intent);
    }
    public void gpsdingwei(){
    manager = (LocationManager) getSystemService(LOCATION_SERVICE);//获取手机位置信息
    sp = getSharedPreferences("canshu",Context.MODE_PRIVATE);    
        List<String> providers = manager.getAllProviders();
        
        for (String provider : providers) {
            System.out.println(provider);
        }
       

       //获取的条件
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);//获取精准位置
        criteria.setCostAllowed(true);//允许产生开销
        criteria.setPowerRequirement(Criteria.POWER_LOW);//消耗大的话,获取的频率高
        criteria.setSpeedRequired(true);//手机位置移动
        criteria.setAltitudeRequired(false);//海拔
        
        //获取最佳provider: 手机或者模拟器上均为gps
        String bestProvider = manager.getBestProvider(criteria, true);//使用GPS卫星
//        String bestProvider = manager.NETWORK_PROVIDER;  

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

        System.out.println("最好的位置提供者是"+bestProvider);
        int i =1;
        
        //parameter: 1. provider 2. 每隔多少时间获取一次  3.每隔多少米  4.监听器触发回调函数
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,1, new MyLocationListener());
        while (location != null& i >0){   
            i = i -1;
            location.getAccuracy();//精确度
            String  latitude = location.getLatitude()+"";//经度
            String longitude = location.getLongitude()+"";//纬度
            //将获取到的经纬度信息存入SharedPreferences
            Editor editor = sp.edit();
            editor.putString("lastloaction", latitude + "-" + longitude);
            editor.commit();
            //报告位置
            String anquantel =  sp.getString("tel", "");
            SmsManager smsManager = SmsManager.getDefault();  
            smsManager.sendTextMessage(anquantel, null, "我现在地址经度(jingdu)为"+latitude + ",维度为" + longitude, null, null);
        }    
    }
    private class MyLocationListener implements LocationListener{
        
        public void onLocationChanged(Location location) {
            location.getAccuracy();//精确度
            String  latitude = location.getLatitude()+"";//经度
            String longitude = location.getLongitude()+"";//纬度
           

            //将获取到的经纬度信息存入SharedPreferences
            Editor editor = sp.edit();
            editor.putString("lastloaction", latitude + "-" + longitude);
            editor.commit();
            //报告位置
            String anquantel =  sp.getString("tel", "");
            SmsManager smsManager = SmsManager.getDefault();  
            smsManager.sendTextMessage(anquantel, null, "我现在地址经度为"+latitude + ",维度为" + longitude, null, null);

        }
        
        
        public void onStatusChanged(String provider, int status, Bundle extras) {
            
        }
        
        
        
        public void onProviderEnabled(String provider) {
            
        }

        
        
        public void onProviderDisabled(String provider) {
            
        }
        
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

经多次在模拟器上调试,发觉个问题,就是location时而为NULL时而找得到,当找不到的会后就肯定报空指针错误,为了避免location包空指针错误,用while跳过。在网上找了很多资料,感觉合理的解析就是getLastKnownLocation这个方法是得到手机或模拟器的最后一次知道的location,但因为模拟器默认状态没有知道的location,所以就是null,然而当用监听器监听了一次或几次后,就有location了。当模拟器出现GPS图标后,根据筛选条件location 100%是GPS。

posted @ 2012-08-08 00:16  qishangui  阅读(893)  评论(0)    收藏  举报