我的APP在收到ACTION_SCREEN_OFF時必須啓動一些耗時的工作,並且在作業仍在繼續時收到ACTION_SCREEN_ON時中斷作業。如何停止Handler在android中工作?
public class TimeConsumingWorkIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
TimeConsumingWork();
}
}
public class ScreenStatusReceiver extends BroadcastReceiver {
Intent intent = new Intent(mContext, TimeConsumingWorkIntentService.class);
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
mContext.startService(intent);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
mContext.stopService(intent);
}
}
}
截止時間打印日誌,我覺得費時的工作仍在繼續停止TimeConsumingWorkIntentService(接收ACTION_SCREEN_ON時)。
爲什麼?
感謝Arju。我之前嘗試過你的方法,它不適用於某些未知的原因(抱歉,我不記得原因)。我嘗試使用IntentService,它也不起作用。 – RoFF
我剛更新了我的代碼:) –