开发者

How to stop Android from stopping my service?

I am using the following code to prevent Android from stopping my service:

开发者_JS百科@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    //return super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

but this stops my service from working. What can I try to achieve this?


Do you have a User Interface or Activity associated to this service? If so, you can create your service such that it runs in the same process as your Activity. In this scenario, your service will only get killed if the application is killed, which usually happens when system is low on memory or other scenarios that the application is killed.

This way of running a service is typically called running a local service. You can find references to running a local service in the Android SDK Sample API's or at http://developer.android.com/reference/android/app/Service.html#LocalServiceSample.

In the local service, rather than overriding the onStartCommand, you override onBind, and in the onBind method you return an IBinder (a remoteable object). The IBinder object will contain a method like getService() which will return a reference to your service. After acquiring a reference to your service, you can call the service's methods directly from within your activity.

Here's the sample code for the Service (I typed this in, so it may not compile but it should convey the concept):

public class LocalService extends Service
{

   @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder{
       public LocalService getService()
       {
           return LocalService.this;
       }

    }

    //other code...
    public void myServiceMethod()
    {
       //do whatever
    }
} 

Sample code for the client:

public class ClientActivity extends Activity
{
   //a handle to our service which we can then make calls to
   private LocalService myLocalService;
   private boolean mIsBound;
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate();
      mIsBound = false;
      //blah blah

     doBindService();

     //sometime later...
     if(mIsBound)
         myLocalService.myServiceMethod();      
   }

   private void doBindService()
   {
      bindService(new Intent(ClientActivity.this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE);   
   }

   private ServiceConnection mConnection = new ServiceConnection() {

   public void onServiceConnected(ComponentName className, IBinder service) {
        //called when service is connected
        myLocalService = ((LocalService.LocalBinder)service).getService();
        mIsBound = true;

    }

    public void onServiceDisconnected(ComponentName className) {
        // called when service is disconnected
        myLocalService = null;
        mIsBound = false;

    }

   }

}


Short answer - you can not stop android from stopping your service and there is very little reason to ever have a persistent service continually running.

If you need to run some code on a scheduled basis for your application, consider using Android's AlarmManager that works similarly to a UNIX cron job.


Use startForeground method instead. From Service JavaDoc:

A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜