2010-12-17 128 views
14

我对我的android应用程序有一个后台服务,它获取我的GPS位置并将其发送到远程数据库。它工作很好。stopService不会停止我的服务....为什么?

问题是当我想停止服务....它不停止:S。也没有例外或logcat出现错误......它根本不停止。

这是开始我srvice代码(一个按钮):

startService(new Intent(GPSLoc.this, MyService.class)); //enciendo el service 

这是我阻止它(在onactivityresult方法)的代码:

stopService(new Intent(GPSLoc.this, MyService.class)); 

我一直调试应用程序,我检查了每次我调试stopService代码行,但它不会停止......

我相信它不会停止导致我的数据库我仍然REC当我按下按钮停止服务时,从模拟器开始使用gps位置。

我在做什么坏事?

回答

25

您是否实施了onDestroy()?如果不是,我相信这可能是解决方案 - 并且您会停止Timer或您在onDestroy()之内运行服务所用的任何内容。

服务可以通过调用其stopSelf()方法或通过调用Context.stopService()来停止。

有关更多信息,请参阅此link

+1

哦,我没有实现的OnDestroy !!!!!!在服务上,我正在使用一个处理程序的线程和一个简单的处理程序。 ¿如何阻止他们? – NullPointerException 2010-12-17 15:04:28

+0

它对我的作品,谢谢 – 2017-04-05 05:49:43

+1

我不明白为什么有必要实现onDestroy()。它只是一个生命周期回调方法。你能澄清一下吗? – likejiujitsu 2017-06-17 21:59:04

0

也许您可能在每次调用停止服务时创建一个新的Intent。

stopService(new Intent(GPSLoc.this, MyService.class)); 

也许尝试:

Intent intnet = new Intent(GPSLoc.this, MyService.class); // create el service 

startService(intenet); 
stopService(intent); 
+0

这段代码不起作用,我试过了。 – user2580401 2013-11-30 20:57:42

8

我确信,它没有停止的原因在我的数据库,我仍然recive从模拟器GPS位置时,我都按按钮停止该项服务。

您可能未注销您的LocationListener

0

对于那些想定期向服务器发送请求的人,这是我的解决方案。你应该在你的活动或片段活动中有这个

{ 
private static final Long UPDATE_LOCATION_TIME = 30 * 60 * 1000l; // 30 minute 

private AlarmManager alarm; 
private PendingIntent pIntent; 
... 

@Override 
    protected void onResume() { 
     super.onResume(); 

     // Run background service in order to update users location 
     startUserLocationService(); 

     Log.e(TAG, "onResume"); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 

     stopUserLocationService(); 

     Log.e(TAG, "onStop"); 
    } 

private void startUserLocationService() { 
     Log.i(TAG, "Starting service..."); 
     Intent intent = new Intent(MainFragmentHolder.this, ServiceUserLocation.class); 
     pIntent = PendingIntent.getService(this, 0, intent, 0); 

     alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     Calendar cal = Calendar.getInstance(); 
     alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), UPDATE_LOCATION_TIME, pIntent); 
    } 

    private void stopUserLocationService() { 
     alarm.cancel(pIntent); 
     Intent intent = new Intent(MainFragmentHolder.this, ServiceUserLocation.class); 
     stopService(intent); 
    } 

} 
2

这是非常普遍的情况,我需要在完成过程之前停止我的服务。在某些情况下,stopService(intent)是不够的。你应该记住我的服务中的onDestroy()实现。例如:

public class MyIntentService extends IntentService { 

    // Defines and instantiates an object for handling status updates. 
    private BroadcastNotifier mBroadcaster = null; 
    private int progress = 0; //THIS IS MY COUNTER FOR EXAMPLE!!! 

    public MyIntentService() { 
     super("MyIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     progress = 0; 
     int tiempo_disponible = intent.getIntExtra("minutos_disponible", 0); 

     if (mBroadcaster == null){ 

      mBroadcaster = new BroadcastNotifier(this); 
     } 
     // Broadcasts an Intent indicating that processing has started. 
     mBroadcaster.broadcastIntentWithState(Constants.STATE_ACTION_STARTED); 

     mBroadcaster.broadcastIntentWithState(Constants.STATE_ACTION_RUNNING); 

     while (progress < tiempo_disponible) { 

      progress++; 
      try { 
       Log.i(Constants.TAG, "Procesing " + progress); 
       mBroadcaster.notifyProgress(progress); 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     // Reports that the feed retrieval is complete. 
     mBroadcaster.broadcastIntentWithState(Constants.STATE_ACTION_COMPLETE); 
    } 

    @Override 
    public void onDestroy() { 
     progress = 1000000; // WHITH THAT YOU FINISH THE CICLE IF tiempo_disponible NEVER IS MAYOR THAT 1000000, YOU CAN USE OTHER CONDITIONAL!!!!!! 
     super.onDestroy(); 
    } 
} 

这样,当您使用stopService方法停止服务时,您也将停止进程o计数器。

public void stopService(){ 
     context.stopService(intent); 
     LocalBroadcastManager.getInstance(context).unregisterReceiver(responseReceiver); 
     responseReceiver = null; 
     intent = null; 
} 

保重! @yaircarreno

+1

您正在使用错误的服务类型。 IntentService是为了管理其生命周期而设计的。 完成onHandleIntent方法的业务逻辑后 - IntentService将自行销毁并释放所有关联的资源。 – 2014-12-11 11:32:11

1

如果你正在跟踪GPS位置,你可能使用GoogleApiClient

的概念是,该服务不会停止,

如果GoogleApiClient实例中它仍然连接。

(或需要被销毁/未注册的第一任何其他问题)

因此,为了使它的工作原理,你的服务中实现onDestroy()

@Override 
public void onDestroy() 
{ 
    // Unregistered or disconnect what you need to 
    // For example: mGoogleApiClient.disconnect(); 
    super.onDestroy(); 
} 
+1

我实际上是试图停止使用谷歌API的服务,我忘了断开API的OnDestroy ...!案例关闭.. – 2017-06-02 07:32:49

0

我的问题通过删除添加视图解决WindowManagerondestroy

public void onDestroy() { 
    isRunning = false; 
    super.onDestroy(); 
    if (checkBox!=null) { 
     windowManager.removeView(getlayoutparm(fabsetting,fabrateus,fabexit,true)); 
     windowManager.removeView(checkBox); 
    } 
} 
3

我有同样的问题。我发现如果服务已连接GoogleApiClient并仍然获取位置更新,则stopService()完全没有影响,但该服务的行业()未被调用。 为了解决这个问题,我创建了一个函数来停止服务代码中的位置服务。从活动中调用stopLocationService(),然后调用stopService。下面是代码示例:

public class myLocationService extends Service{ 
... 

    public void stopLocationUpdates() { 

     LocationService.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);  
     mGoogleApiClient.disconnect(); 

    } 
    ... 
} 

在活动中,

{ 
    ... 
    if(mService != null && isBound) { 

     mService.stopLocationUpdates(); 
     doUnbindService(); 
     stopService(new Intent(this, myLocationService.class)); 

    } 
    ... 
} 
0

在我的情况下stopService被称为与startService几乎同时所以没有服务是有停止。尝试延迟stopService几秒钟。 :)

0

@覆盖

public void onDestroy() { 

    Log.d(TAG, "onDestroy"); 
    super.onDestroy(); 
    if (mLocationManager != null) { 

     for (int i = 0; i < mLocationListeners.length; i++) { 

      try { 

       mLocationManager.removeUpdates(mLocationListeners[i]); 

      } catch (Exception ex) { 

       Log.d(TAG, "fail to remove location listners, ignore", ex); 

      } 

     } 

    } 

} 
+0

在公共类MyService extends Service中尝试此操作 – 2017-04-05 05:51:25