-1

I am trying to implement something inside AsyncTask like this.

@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);?

Community
  • 1
  • 1
TRX
  • 465
  • 1
  • 8
  • 17
  • runOnUIThread() try using it – A_rmas Mar 29 '16 at 03:30
  • 1
    @ArmasCyndromic there is no reason to use `runOnUiThread()` with `AsyncTask` since it already provides methods to run code on the main `Thread` – codeMagic Mar 29 '16 at 03:35
  • @codeMagic true there are methods that is used to run code on main thread. I believe runOnUiThread() is that method that can give access to main thread even form some of the background tasks on need e.g doInBackground() in async task. – A_rmas Apr 07 '16 at 11:53
  • 1
    @ArmasCyndromic true but I've seen it cause too many problems for folks which is unnecessary since `AsyncTask` was created to solve these issues. If you need to update the UI from `doInBackground()` before the task finishes, there is no reason to introduce more complicated and possibly introduce more bugs with that code when you can simply call `publishProgress()`. If the task is done, then obviously `onPostExecute()` will be called to update the UI. – codeMagic Apr 07 '16 at 12:58

3 Answers3

5

As you can see from the docs getWindow() is a method of Activity. And, since you aren't in an Activity, the method is not available.

So you need a reference to the Activity to recognize this method.

Now, I can't see much of your code but you can pass the Context from the Activity and cast that to reference the method.

So you would pass the Context like this then cast it to Activity

This SO answer should give you a little more help

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Thanks, I have try get `context` in my `AsyncTask` `context = progressDlg.getContext();` cast it in `onPreExecute` and `onPostExecute`, but get run-time error. – TRX Mar 29 '16 at 04:21
  • @TRX you aren't passing a `Context` to your `AsyncTask` so I suppose your `Context` variable is `null`. [See this answer](http://stackoverflow.com/questions/15409912/should-i-use-asynctask-class-inside-the-mainactivity-extending-activity/15409989#15409989) on passing it to your task constructor. – codeMagic Mar 29 '16 at 16:54
2

getWindow() is a method available from your Activity.

This will be available to you via the ff:

  • if your AsyncTask is within/inside your Activity.
  • getActivity().getWindow()
codeMagic
  • 44,549
  • 13
  • 77
  • 93
ekouChiq
  • 244
  • 2
  • 10
  • My `AsyncTask` is not inside `Activity`, but a separate class, the caller is a `Fragment` – TRX Mar 29 '16 at 04:21
  • 1
    You can achieve this in different ways. For example, you can pass your `Activity` to your `AsyncTask` as per @codeMagic, and from there, you can call `yourActivity.getWindow()`. Please remember that `getWindow()` method will only be accessible to you from your `Activity` or if you have reference to an `Activity`. – ekouChiq Mar 29 '16 at 04:38
0

Try :

yourActivity.this.getWindow().clearFlag (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Here is a ref that can help you more: link

Community
  • 1
  • 1
Sayem
  • 4,891
  • 3
  • 28
  • 43