我的服务没有停止

时间:2014-05-14 17:10:43

标签: android

每次我的服务需要阻止它只是重新启动它自己。它只会在我强制停止时停止,但程序上不会停止。

这是我的服务:

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;       
        Location location;// location
        Location sp;
        double latitude; // latitude
        double longitude; // longitude
        double line;
        double ship  ;
        double b   ;
        double[] d = new double[1000];
        String numberr;
        WakeLock wakeLock;
    @Override
    public void onCreate() {
        super.onCreate();

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");
        final LocationListener mlocList = new MyLocationList();
        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 (isNetworkEnabled) {
                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
                if (isGPSEnabled) {
                if (location == null) {
                    lm.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                0,
                                0,  mlocList);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (lm != null) {
                        location = lm
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);

                            }
                    }
                }
            }

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = lm.getBestProvider(criteria, true);
       // Location mostRecentLocation = lm.getLastKnownLocation(provider);
        UpdateWithNewLocation(location); 
    }
    @Override
    public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

     wakeLock.release();
     stopSelf();
    }


    @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]+"THIS IS IT");
        }
        else
        {
            Log.i("Log", "Bundle is null");
        }   
        return START_STICKY;

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

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

        ship=(d[2]-d[0])/(d[3]-d[1]);
        b=d[0]-ship*d[1];
        line=ship*lat+b-Long; 
         Toast toast = Toast.makeText(getBaseContext(),  String.valueOf(lat)+"AND"+String.valueOf(Long), Toast.LENGTH_LONG);
            toast.show();
            System.out.println(b);
            System.out.println(ship);
            System.out.println(line);
           if (line>-1 && line<1){
              SmsManager.getDefault().sendTextMessage(numberr, null, "Elvis Just Left The building", null, null);
             onDestroy(); 
        }
        }

        else 
        {
             String latLongStr = "No lat and longitude found";
             Toast.makeText(this, "Your location is "+latLongStr ,Toast.LENGTH_LONG).show();
        }


    }


     public class MyLocationList implements LocationListener
     {

     public void onLocationChanged(Location arg0) {
         // TODO Auto-generated method stub
         UpdateWithNewLocation(arg0);
     }

     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;
    }
}

任何想法为什么?我使用部分唤醒锁。

1 个答案:

答案 0 :(得分:1)

你使用了START_STICKY。你应该使用START_NOT_STICKY。你也应该在完成操作后调用stopSelf()!不要调用onDestroy()并从那里调用stopSelf():-) 请暂停您的开发一分钟并阅读:http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle

  

此服务将在此时继续运行,直到调用Context.stopService()或stopSelf()。