我的服务疯了

时间:2014-05-17 11:06:40

标签: android

我的服务是不停地发送非停止短信息....他不关心Stopself()命令......它只是继续运行,即使我摧毁它... 这真烦人......

这就是我的服务代码:

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

        // flag for network status
        boolean isNetworkEnabled = false;

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

        double lat; // latitude
        double Long; // longitude
        double line;
        double ship  ;
        double b   ;
        double[] d = new double[4];
        String numberr;
        WakeLock wakeLock;
        final LocationListener mlocList = new MyLocationList();


    @Override
    public void onCreate() {
        super.onCreate();
        LocationManager lm = (LocationManager) getSystemService(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,  mlocList);


                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,  mlocList);
                    Toast toast = Toast.makeText(getBaseContext(),  "WOW", Toast.LENGTH_LONG);
                    toast.show();
                        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()) {  // has effect of unparcelling Bundle


            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");

        }
        else
        {
            Log.i("Log", "Bundle is null");
        }

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

         canGetLocation= inRange(d[1],d[0],d[3],d[2],Long,lat); 
       System.out.println(canGetLocation);

         if (canGetLocation){


         SmsManager.getDefault().sendTextMessage(numberr, null, "I'm", null, null);     
          if (lm!=null){

          lm.removeUpdates(mlocList);
         lm=null;
          }

          stopSelf(); 

         }
              }
        return START_STICKY;

    }

     public class MyLocationList implements LocationListener
     {


    public void onLocationChanged( Location pocation) {

         if (pocation != null){
         lat=pocation.getLatitude();
         Long=pocation.getLongitude();


         canGetLocation= inRange(d[1],d[0],d[3],d[2],Long,lat); 
         if (canGetLocation){

         SmsManager.getDefault().sendTextMessage(numberr, null, "I'm In the line area!", null, null);

          if (lm!=null){

          lm.removeUpdates(mlocList);
         lm=null;
          }

          stopSelf(); 
            }
         }
     }

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

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

     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(mlocList);
              lm=null;

              } 



    }
    public static boolean inRange(double start_x, double start_y, double end_x, double end_y,
            double point_x, double point_y) {
double dx = end_x - start_x;
double dy = end_y - start_y;
double innerProduct = (point_x - start_x)*dx + (point_y - start_y)*dy;
return 0 <= innerProduct && innerProduct <= dx*dx + dy*dy;
}


} 

请帮忙......

1 个答案:

答案 0 :(得分:0)

只需将START_STICKY替换为START_NOT_STICKY

即可

Documentation for START_STICKY

相关问题