Pregunta

I'm building an application that has a widget and a button in the widget launches an IntentService. The onHandleIntent() runs some code and then raises a toast through a handler. After I click the button in the widget, I see the toast and I know that onHandleIntent finished. But when I look in the background services I still see my app there.

As a user, I get very annoyed when apps always run on the background and wasting my precious RAM. My widget doesn't need to run in the background because my widget doesn't update ever (the update rate in the xml is 0).

So how come that the service is still running? How can I stop it?

IntentService code:

public class WidgetCheckService extends IntentService {
    private int mAppWidgetId;
    Handler mHandler = new Handler();

    public SalaryWidgetCheckService(String name) {
        super("laceService");
    }

    public SalaryWidgetCheckService() {
        super("laceService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // Badass code
            mHandler.post(new DisplayToast(getString(R.string.widget_service_check_in_success_toast_text).replace("LACE", lace)));
        else mHandler.post(new DisplayToast(getString(R.string.widget_service_check_out_success_toast_text).replace("LACE", lace)));
    }

    private class DisplayToast implements Runnable{
        String mText;

        public DisplayToast(String text){
            mText = text;
        }

        public void run(){
            Toast.makeText(WidgetCheckService.this, mText, Toast.LENGTH_SHORT).show();
        }
    }
}

Thanks, Elad.

¿Fue útil?

Solución

But when I look in the background services I still see my app there.

Your process is not automatically terminated when the service ends. Android will keep your process running until it needs to free up the RAM for other apps, just in case it happens to need to run something from your app again. This is no different than any other app on Android.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top