1
我正在構建具有小部件的應用程序,並且小部件中的按鈕會啓動IntentService。 onHandleIntent()運行一些代碼,然後通過處理程序引發敬酒。 當我點擊小部件中的按鈕後,我看到敬酒,並且我知道onHandleIntent已完成。 但是當我查看後臺服務時,我仍然在那裏看到我的應用程序。即使在onHandleIntent完成後,IntentService仍會在後臺運行
作爲一個用戶,當應用程序總是在後臺運行並浪費我珍貴的RAM時,我非常惱火。我的小部件不需要在後臺運行,因爲我的小部件不會更新(xml中的更新速率爲0)。
那麼爲什麼服務仍在運行?我怎樣才能阻止它?
IntentService代碼:
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();
}
}
}
謝謝,埃拉德。
好的,謝謝澄清! – Elad92 2012-03-07 17:28:13