2014-05-14 2 views
2

서비스를 중지해야 할 때마다 다시 시작됩니다. 멈출 때만 멈추지 만 프로그래밍 방식으로 멈추지는 않습니다.내 서비스가 중지되지 않습니다

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

START_STICKY를 사용했습니다. START_NOT_STICKY를 사용해야합니다. 또한 작업을 마쳤을 때 stopSelf()를 호출해야합니다! 잠시 개발을 일시 중지하고 읽어 보시기 바랍니다들의 OnDestroy을 (전화) 및 :-) stopSelf() 거기에서하지 마십시오이 : http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle

서비스는이 시점에서 Context.stopService() 또는 stopSelf 때까지 계속 실행됩니다()가 호출됩니다.

+0

감사합니다. 이제 앱이 중지되었다는 메시지가 표시됩니다. 해당 메시지를 표시하지 않고 서비스를 중지 할 수 있습니까? – mynavy

+1

아마도 크래시, 예외 ... 당신은 이클립스 디버거와 함께 응용 프로그램을 실행하거나 logcat 메시지를 봐야합니다. 거기에 문제가 무엇인지 보여주는 몇 가지 역 추적 표시되어야합니다. 예 : https://www.youtube.com/watch?v=JqHYbm9e05A – RumburaK

+0

감사합니다. 방금 wakelock을 사용 중지하고 이제 부드럽게 진행합니다.하지만 어쨌든 서비스는 결코 파괴되지 않습니다 (내 새로운 문제가 있습니다) – mynavy

관련 문제