没有得到真正的位置更新

时间:2014-05-17 07:35:42

标签: android

我在我的服务中实现了一个位置监听器,但不知何故我没有获得真正的位置更新....

这就是我的服务代码:

public class GpsTracker extends Service implements LocationListener  {
    // flag for GPS status
        boolean isGPSEnabled = false;

        // flag for network status
        boolean isNetworkEnabled = false;

        // flag for GPS status
        boolean canGetLocation = false;
        //protected LocationManager lm;     
         Location location;// location
         LocationManager lm ;

        double lat; // latitude
        double Long; // longitude
        double line;
        double ship  ;
        double b   ;
        double[] d = new double[4];
        String numberr;


    @Override
    public void onCreate() {
        super.onCreate();
    LocationManager lm =(LocationManager)getSystemService(Context.LOCATION_SERVICE);

    isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);


        // getting network status
        isNetworkEnabled = lm
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!isGPSEnabled && !isNetworkEnabled) {

            // no network provider is enabled
            } else {

            if (!isGPSEnabled) {
                lm.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        0,
                        0,  this);

                Log.d("Network", "Network");
                if (lm != null) {
                    location = lm
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    }
                }
            // if GPS Enabled get lat/long using GPS Services
            else  {
                if (location == null) {
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);



            Log.d("GPS Enabled", "GPS Enabled");
            if (lm != null) {
            location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);


                       }
                    }
                 }
              }     
           }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

         Bundle extras = intent.getExtras();


        if (extras != null && !extras.isEmpty()) {  


            String message = intent.getStringExtra("message");
             numberr = intent.getStringExtra("number");


            int p=0,j,i=1,t,success=0;


            Matcher m = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(message);

            while(m.find())
            {
               double  k = Double.parseDouble(m.group(1));
               d[p]=k;
               p++;
               }
          System.out.println(d[0]+","+d[1]+":"+d[2]+","+d[3]+"THE REAL DEAL");
          ship=(d[2]-d[0])/(d[3]-d[1]);
            b=d[0]-(ship*d[1]);
        }
        else
        {
            Log.i("Log", "Bundle is null");
        }

        UpdateWithNewLocation(this.location);
        return START_STICKY;

    }

     private void UpdateWithNewLocation(Location loc) {
        // TODO Auto-generated method stub


        if(loc!= null )
        {
         lat =loc.getLatitude(); // Updated lat
         Long = loc.getLongitude(); // Updated long

         line=(ship*Long)+(b-lat); 
         Toast shoast = Toast.makeText(getBaseContext(), String.valueOf(line), Toast.LENGTH_LONG);
            shoast.show();
            System.out.println(b);
            System.out.println(ship);
            System.out.println(line);

           if (line>(-0.001) && line<(0.001)){

              SmsManager.getDefault().sendTextMessage(numberr, null, "I Just", null, null);

              if (lm!=null){

              lm.removeUpdates(this);
             lm=null;
              }
              stopSelf(); 

        }
        }
        else 
        {
             String latLongStr = " N/A !";
             Toast.makeText(this, "Your location is "+latLongStr ,Toast.LENGTH_LONG).show();
        }


    }


    public void onLocationChanged( Location pocation) {
         // TODO Auto-generated method stub
         //Toast toast = Toast.makeText(getBaseContext(),  String.valueOf(location.getLatitude())+"  "+String.valueOf(location.getLongitude()), Toast.LENGTH_LONG);

         if (pocation != null){
         this.location = pocation;
         UpdateWithNewLocation(this.location);
         }
     }

     public void onProviderDisabled(String provider) {
         // TODO Auto-generated method stub
         Toast.makeText(getApplicationContext(),"GPS Disable ", Toast.LENGTH_LONG).show();
     }

     public void onProviderEnabled(String provider) {
         // TODO Auto-generated method stub
         Toast.makeText(getApplicationContext(),"GPS enabled", Toast.LENGTH_LONG).show();
     }

     public void onStatusChanged(String provider, int status, Bundle extras) {
         // TODO Auto-generated method stub

     }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onDestroy() {
    // TODO Auto-generated method stub
        super.onDestroy();
         if (lm!=null){
              lm.removeUpdates(this);
              lm=null;

              } 
    }
}

当我在街上测试它时它只是没有更新..有时它只是卡住了,我没有看到任何更新。不是在Gps而不在网络中。

1 个答案:

答案 0 :(得分:0)

它不起作用,因为如果您使用位置监听器启动服务,则至少会遇到两个问题:服务可能会被操作系统杀死,CPU可能会进入深度睡眠状态。要解决这些问题,您必须使用前台服务,如果需要,可以使用唤醒锁以避免深度睡眠。此外,您可以考虑使用pendingIntent来接收位置信息。通过这种方式,您可以创建自己的广播接收器,并在收到新位置时启​​动IntentService。 其他一点:您正在使用0,0作为参数注册位置更新,您应该传递时间和距离的最小值,您确定要延迟0吗?我想这个应用程序很快就会耗尽你的电池。