Sercan Aktan
Sercan Aktan

Reputation: 241

Android prevent working on background

I wrote a service which is registering a Smsreciever, and always work on background. Also, i wanna prevent my main activity to working on background while not using the program. Generally it is working but sometimes my activity starting and running at background after recieved sms, it causes battery consumption, how can i prevent that

public void onCreate()
{
    ...

    startService(new Intent(this,ServiceCommunicator.class));

    ...
}               

 public void onBackPressed() {

    super.onBackPressed();
    android.os.Process.killProcess(android.os.Process.myPid()) ;

      }

Upvotes: 0

Views: 82

Answers (1)

android developer
android developer

Reputation: 116322

some notes about what you are doing:

  1. it is not advised to kill your process . just let the service continue . when you call "startService" it should continue even after the activity was closed.

  2. background service can , by definition, die whenever the system thinks it needs memory . if you don't want to miss sms events , make your service run on the foreground .

  3. the more cpu and battery your service uses , the more chance the os will decide to kill it . services should be as lite as possible , and if they do a lot of work for a long time , they should be in the foreground .

  4. if your activity uses a lot of CPU , your service might also be closed when closing the app , since they both belong to the same process. you can change the process of the service via the manifest.

Upvotes: 1

Related Questions