使用GPS定位为什么location总为空 而且onLocationChanged()方法没调用呀

package com.example.gpstest;

 

import java.text.BreakIterator;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;


import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.text.Html.TagHandler;
import android.util.Log;
import android.webkit.WebIconDatabase.IconListener;

public class GPSLocationService extends Service implements LocationListener{
 
 private static final String TAG="GPSLocationService";
 //通知工具类
 private NotificationUtil mNotificationUtil;
 private Context mContext;
 private Timer timer;
 private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
 private static final int OPENGPS=1; 
 private static final int NONGPS=2; 
 private String provider;
 private boolean is_openGPS=true;
 String latLongString="";
 private Handler mHandler=new Handler(){
  
  @Override
  public void handleMessage(Message msg) {
   // TODO Auto-generated method stub
   
   switch (msg.what) {
   case 1:
    /*mNotificationUtil.cancelNotification(NotificationUtil.NOTIFICATIO_GPS_NOT_OPEN);
    is_openGPS=false;*/
    locationManager.requestLocationUpdates(provider, 1000, 10, GPSLocationService.this);
           

              Log.i(TAG, "进入providerprovider");
              location = locationManager.getLastKnownLocation(provider);
             
              Log.i(TAG, "进入is_openGPS");
            
              //locationManager.requestLocationUpdates(provider, 2000, 10, GPSLocationService.this);
 
              updateWithNewLocation(location);
   
    break;
   case 2:
    mNotificationUtil.sendGPSNotification();
    break;

   default:
    break;
   }
   super.handleMessage(msg);
  }
 };
 private LocationManager locationManager;
 private Location location;
 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  return null;
 }
   @Override
 public void onCreate() {
    // TODO Auto-generated method stub
    
  super.onCreate();
    GPSReceiver gpsReceiver=new GPSReceiver();
          IntentFilter filter=new IntentFilter("gpstest.gpsreceiver");
          registerReceiver(gpsReceiver, filter);
        
    mNotificationUtil = new NotificationUtil(this);

         mContext = GPSLocationService.this;
        
         timer = new Timer(true);
        
        /* timer.schedule(new TimerTask() {
    
    @Override
    public void run() {
     // TODO Auto-generated method stub
     if(GPSUtil.isOPen(GPSLocationService.this)){
       Log.i(TAG, "进入is_openGPS");
      Message message = mHandler.obtainMessage();
      message.what=OPENGPS;
      mHandler.sendMessage(message);
      
     }else{
       Log.i(TAG, "进入NONGPS");
      Message message = mHandler.obtainMessage();
      message.what=NONGPS;
      mHandler.sendMessage(message);
     };
    }
   }, 1000*60*30, 1000);*/
        

         //用户已经打开GPS,然后可以使用GPS定位
         if(is_openGPS){
          
          timer.schedule(new TimerTask() {
     
     @Override
     public void run() {
      // TODO Auto-generated method stub
      if(GPSUtil.isOPen(GPSLocationService.this)){
        Log.i(TAG, "进入is_openGPS");
        // 定位管理器
              locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
              

              //设置不需要查询条件定位
              locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
              Criteria criteria = new Criteria();
              // 获得最好的定位效果 
                 criteria.setAccuracy(Criteria.ACCURACY_FINE); 
                 criteria.setAltitudeRequired(false); 
                 criteria.setBearingRequired(false); 
                 criteria.setCostAllowed(false); 
                 // 使用省电模式 
                 criteria.setPowerRequirement(Criteria.POWER_LOW); 
                  provider =locationManager.getBestProvider(criteria, true);

                 Message message = mHandler.obtainMessage();
        message.what=OPENGPS;
        mHandler.sendMessage(message);
                
      }else{
        Log.i(TAG, "进入NONGPS");
       Message message = mHandler.obtainMessage();
       message.what=NONGPS;
       mHandler.sendMessage(message);
      };
     }
    }, 1000, 1000);
          
          
         }
  
 }
   
   
   
     private void updateWithNewLocation(Location location) {
           
           
         
             Log.i(TAG, "进入updateWithNewLocation");
             if (location != null) {
                     double lat = location.getLatitude();
                     double lng = location.getLongitude();
                     float spe = location.getSpeed();// 速度
                    float acc = location.getAccuracy();// 精度
                    double alt = location.getAltitude();// 海拔
                    float bea = location.getBearing();// 轴承
                  
                    long tim = location.getTime();// 返回UTC时间1970年1月1毫秒
                    latLongString = "纬度:" + lat + "\n经度:" + lng + "\n精度:" + acc
                                     + "\n速度:" + spe + "\n海拔:" + alt + "\n轴承:" + bea + "\n时间:";
                  
                    
             } else {
                     latLongString = "无法获取位置信息";
             }
           //  myLocationText.setText("您当前的位置是:\n" + latLongString);
             Intent intent=new Intent("result.request.latLongString");
             intent.putExtra("latLongString", latLongString);
             sendBroadcast(intent);
            
             Log.i(TAG, latLongString);
     }
 @Override
 public void onLocationChanged(Location location) {
  // TODO Auto-generated method stub
   Log.i(TAG, "onLocationChanged");
  updateWithNewLocation(location);
   Log.i(TAG, "onLocationChanged");
     
 }
 @Override
 public void onProviderDisabled(String provider) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void onProviderEnabled(String provider) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
  // TODO Auto-generated method stub
  
 }


 class GPSReceiver extends BroadcastReceiver{

  @Override
  public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub
   intent.setAction("result.request.latLongString");
   intent.putExtra("latLongString", latLongString);
   sendBroadcast(intent);
  }

}


 
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值