SoulRayder
SoulRayder

Reputation: 5166

Restarting a service in the onDestroy method

I have made an app in which a service runs in the background. But if the android system requires resources, it will stop the service. However I may still require my service to run.

Is it a bad practice to restart the service (if condition relevant to my app still holds true) in the onDestroy method of my service?

How can I make sure my service runs indefinitely (if condition relevant to my app still holds true)? Or atleast on high priority?

Upvotes: 2

Views: 5909

Answers (2)

mhk
mhk

Reputation: 112

set it START_STICKY. It Causes after killing service the service will restart again. it is my code :

android manifest : 
<application 
....
    <service android:name=".UpdateService" />

 </application>

service class :

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


public class UpdateService extends Service {
BroadcastReceiver mReceiver;


@Override
public void onCreate() {
    super.onCreate();
    // register receiver that handles screen on and screen off logic
    IntentFilter filter = new IntentFilter(Intent.....);

    filter.addAction(Intent....);
    mReceiver = new MyReceiver();
    registerReceiver(mReceiver, filter);
}


@Override
public void onDestroy() {

    unregisterReceiver(mReceiver);
    Log.i("onDestroy Reciever", "Called");

    super.onDestroy();
}


@Override
public void onStart(Intent intent, int startId) {

        Log.i("log", "action Called");

    }
}


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_STICKY;
    }
}

receiver class :

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

        Log.i("Log", "recevid");

}
}

in StartupActivity :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Context context = getApplicationContext();
    Intent service = new Intent(context, UpdateService.class);
    context.startService(service);
}

Upvotes: 1

Jim
Jim

Reputation: 10288

Probably the best you can do is use the START_STICKY flag, which tells Android to attempt to restart the service if it has stopped. Beyond that ensure that it consumes as few resources as possible, so that it is less likely to be destroyed.

Android prioritizes the UI over everything. Then processes that are related to the UI. Then processes that are consuming the least amount of resources. A Service runs in the background, so unless it has resources that are also in use on the UI or connected to the UI in some way, it should be a lower priority.

Also you cannot tell Android how to prioritize your Service (everyone would make theirs the "highest priority" right?). So it goes by how well you minimize the impact on overall resources - why kill 3 Services when it could kill 1 and regain all the resources it needs?

To help understand how to manage memory better: http://developer.android.com/training/articles/memory.html

Upvotes: 1

Related Questions