首页 新闻 会员 周边

Android 获取GPS VM可以,把包打在手机上不可以,求救

0
悬赏园豆:5 [已关闭问题] 关闭于 2013-08-05 17:12
  1 private void initGPS()
  2         {
  3             //通过系统服务,取得LocationManager对象    GPS
  4             loctionManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
  5             /**
  6              * 第一种方法
  7              *   通过GPS位置提供器获得位置(指定具体的位置提供器)
  8              * **/
  9             //通过GPS位置提供器获得位置(指定具体的位置提供器)
 10             /*String provider=LocationManager.GPS_PROVIDER;
 11             Location location = loctionManager.getLastKnownLocation(provider);*/
 12             /**
 13              * 第二种方法
 14              *  使用标准集合,让系统自动选择可用的最佳位置提供器,提供位置
 15              * **/
 16             //使用标准集合,让系统自动选择可用的最佳位置提供器,提供位置
 17             criteria = new Criteria();
 18             criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度
 19             criteria.setAltitudeRequired(false);//不要求海拔
 20             criteria.setBearingRequired(false);//不要求方位
 21             criteria.setCostAllowed(true);//允许有花费
 22             criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
 23             
 24             //从可用的位置提供器中,匹配以上标准的最佳提供器
 25             provider = loctionManager.getBestProvider(criteria, true);
 26             
 27             loctionManager.requestLocationUpdates(provider, 30000, 0, locationListener);
 28             //loctionManager.setTestProviderEnabled(provider, true);
 29             //获得最后一次变化的位置
 30             Location location = loctionManager.getLastKnownLocation(provider);
 31             updateLocation(location);
 32             //return location;
 33         }
 34         
 35         //位置监听器
 36         private final LocationListener    locationListener = new LocationListener()
 37         {
 38             @Override
 39             public void onStatusChanged(String provider, int status, Bundle extras)
 40             {
 41             }
 42         
 43             @Override
 44             public void onProviderEnabled(String provider) {
 45             }
 46             @Override
 47             public void onProviderDisabled(String provider) {
 48             }
 49             //当位置变化时触发
 50             @Override
 51             public void onLocationChanged(Location location) 
 52             {
 53                 updateLocation(location);              
 54                 }
 55             };
 56             ///////////////    
 57         
 58             public void updateLocation(Location location)
 59             {
 60                 if(location!=null)
 61                 {
 62                     TrGpsInfo trGPSInfo=new TrGpsInfo();
 63                     Log.v("机器码:", getLocaldeviceId(con));
 64             Log.v("经度:", Double.toString(location.getLongitude()));
 65             Log.v("纬度:", Double.toString(location.getLatitude()));
 66             
 67             trGPSInfo.setTgin_phone_id(getLocaldeviceId(con));
 68             trGPSInfo.setTgin_longitude(location.getLongitude());
 69             trGPSInfo.setTgin_latitude(location.getLatitude());
 70             //showDialogUp("机器码:"+getLocaldeviceId(con)+"经纬度:"+Double.toString(location.getLongitude())+"<>"+Double.toString(location.getLatitude()));
 71             HttpClientService svr = new HttpClientService(
 72                     AppConstants.SERVER_ADDR
 73                             + con.getString(R.string.SHISHI_GPS_ADD));
 74             svr.addParameter("parameter", GsonUtil.getGson().toJson(trGPSInfo));
 75             
 76             HttpAndroidTask gpsTask = new HttpAndroidTask(con, svr,
 77                     new HttpResponseHandler() {
 78                         // 响应事件
 79                         public void onResponse(Object obj) {
 80                                 JsonEntity jsonEntity = GsonUtil.parseObj2JsonEntity(obj,con,false);
 81                                 if (jsonEntity.getStatus() == 1) {
 82                                    Log.v("对的", "对的");
 83                                 } else {
 84                                     // 无数据返回
 85                                             }
 86                                         }
 87             
 88                                 });
 89                         gpsTask.execute(new String[] {});
 90                     }
 91             }
 92         
 93          /* 获取机器唯一标识  
 94          * @param _context  
 95          * @return  
 96          */  
 97         public String getLocaldeviceId(Context _context){  
 98             TelephonyManager tm = (TelephonyManager) _context  
 99                     .getSystemService(Context.TELEPHONY_SERVICE);  
100             String deviceId = tm.getDeviceId();  
101             if (deviceId == null  
102                     || deviceId.trim().length() == 0) {  
103                 deviceId = String.valueOf(System  
104                         .currentTimeMillis());  
105             }  
106             return   deviceId ;  
107         }

就上面这几个函数,我把InitGPS()放在onCreat()中,在DDMS中可以获取经纬度,在手机上就不可以了。不知所以然啊,Manifest文件中的权限改加的都加了。求指点,感激不尽。

owner_hzh的主页 owner_hzh | 初学一级 | 园豆:6
提问于:2013-07-23 17:49
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册