11

I have an app that has an Activity, which is used as the regular GUI, and a Service. My activity has two buttons. One button to stop the process and one to kill the process. I use these to methods, respectively, to start and stop my process:

Intent i = null;
Button start;
Button stop;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    i = new Intent(this, Service.class);
    start = (Button) findViewbyId(R.id.start_button);
    stop = (Button) findViewById(R.id.stop_button);

    start.setOnClickListener(new OnClickListener(){

        public void onClick(){
            startService(i);
        }
    }
    stop.setOnClickListener(new OnClickListener(){

        public void onClick(){
            stopService(i);
        }
    }
}

This Service is not bound to the actvivty or app. I have the service configured in the Manifest as so:

<service
    android:name="com.example.mypackage.Service"
    android:process=":remote"> 
    <intent-filter>
        <action
            android:name="com.example.mypackage.Service" />
    </intent-filter>
</service>

When I start the Service it runs on it's own independent on anything else. Meaning, when I start the service and onDestroy() the app, the service is still running. I can see it is still running because through adb, I run the ps command and it says it is.

The problem is when I call stopService(intent). When I call stopService(intent) from the activity side, it will run the lines of code set in the onDestroy() of the service, but when I run ps through adb, it says that it's still running.

I want to be able to COMPLETELY destroy the service. I'm not sure how services work, so please, don't hesitate to talk remedial. I'm not sure if it has to do with what I'm doing within the service or not. Thanks!

EDIT: When I start the service, I use the onStartCommand() and run some code there which it does. I also return START_STICKY from onStartCommand(). I also tried returning START_NOT_STICKY and the service is still running after I call startService(intent).

Rob Avery IV
  • 3,562
  • 10
  • 48
  • 72
  • How did you start your Service, I guess in onStartCommand method, you return START_STICKY for your Service, that why after you stop the Service, it will start again, try to return START_NOT_STICKY instead, hope this helps. – user2652394 Dec 16 '13 at 14:19
  • @user2652394 yes, I return `START_STICKY` – Rob Avery IV Dec 16 '13 at 14:21
  • Hmm...are you calling super.onDestroy() in your onDestroy method? – MobileMon Dec 16 '13 at 15:22
  • @MobileMon yup. that's the last thing i do in onDestroy – Rob Avery IV Dec 16 '13 at 15:23
  • 2
    Android is **designed** to preserve a process for potential re-use. You **should not be trying to defeat the operating system by killing it**, except in extremely unusual situations. Primarily on Android, you should expect something unneeded to be *inactive* but *not necessarily dead*. – Chris Stratton Dec 16 '13 at 15:31
  • @ChrisStratton interesting Chris, I wonder if after so much inactive time the OS will completely kill the process. Also, where did you find this knowledge? – MobileMon Dec 16 '13 at 15:38
  • 1
    @MobileMon - this is basic android awareness, quite parallel to the Activity Life Cycle though in your case not an Activity. The process can be kept around or disposed of at the convenience of the system - generally it would be kept unless the memory it was holding was needed for something else. Android uses unix concepts like processes and users in somewhat novel ways, which developers need to become aware of. – Chris Stratton Dec 16 '13 at 15:40
  • @ChrisStratton So, a follow up question: If the service process is already running (after starting it and eventually stopping it the first time), and I want to start the process a second time, does the `onStartCommand()` get called when I call `startService()` a second time? – Rob Avery IV Dec 16 '13 at 15:43
  • http://developer.android.com/reference/android/app/Service.html – Chris Stratton Dec 16 '13 at 15:46
  • [Check the process name contains `:remote` then go ahead](https://stackoverflow.com/a/62148880/4694013) – Anoop M Maddasseri Jun 02 '20 at 10:07

3 Answers3

7

If you want kill service process forcefully, you can use following code:

Process.killProcess(Process.myPid());
António Almeida
  • 9,620
  • 8
  • 59
  • 66
Chari D
  • 91
  • 1
  • 4
6

Services are stopped after stopService is called. After stop, they are inactive, but cached.

By android documentation they become an empty process:

A process that doesn't hold any active application components. The only reason to keep this kind of process alive is for caching purposes, to improve startup time the next time a component needs to run in it. The system often kills these processes in order to balance overall system resources between process caches and the underlying kernel caches.

Service will remain in cache until it will be called by you app again or sytem cache will not have room for it to keep. RS command does not know difference between active and cache dprocess and allways shows all awailible processes. This behaviour will maintain if you remove :remote(note that separate process will not be created)

António Almeida
  • 9,620
  • 8
  • 59
  • 66
Yarh
  • 4,459
  • 5
  • 45
  • 95
-1

I think you could set android:exported="false" in your manifest so that other apps cannot access the service thereby starting the service. I may need to see the full implementation of your code to fully determine the cause of it. I did the same thing and it is working perfectly well.

stkent
  • 19,772
  • 14
  • 85
  • 111