I am trying to implement something inside `AsyncTask` like [this](https://stackoverflow.com/questions/7195423/keep-screen-bright-while-in-asynctask). 

```
@Override
protected void onPreExecute() {
    super.onPreExecute();
    ...
    // prevent screen sleep
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

@Override
protected void onPostExecute(List<Result> results) {
    super.onPostExecute(results);
    getWindow().clearFlag (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    ...
}

```

But I get the error "`Cannot resolve method getWindow()`" in Android Studio. What am I missing?

**Update**

The constructor of my AsyncTask is:

```
public MyTask(ProgressDialog pDlg) {
    progressDlgReference = new WeakReference<>(pDlg);
    progressDlg = progressDlgReference.get();
    context = progressDlg.getContext();
    ...
}

```

I try to cast `context` to `Activity` but got run-time error with `((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);`?