Android 台灣中文網
標題: (急)如何在asynctask中使用getWindowManager() [打印本頁]
作者: cchahaliu 時間: 2013-8-12 12:50
標題: (急)如何在asynctask中使用getWindowManager()
本帖最後由 cchahaliu 於 2013-8-12 12:52 編輯
目前我查出在asynctask中似乎不能使用getWindowManager()的功能,因為getWindowManager()只能在activity使用
那請問要怎麼用activity的方式把getWindowManager()的資料給asynctask呢?
另外asynctask有辦法用getSystemService()來使用getWindowManager()嗎?該怎麼撰寫程式碼呢?
以下是我希望使用的code
private String getWeithAndHeight(String url) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
String w=new String(""+width);
String h=new String(""+height);
url=url+"&23=w%3A"+w+"%20h%3A"+h+"%20d%3A";
return url;
}
非常感謝各位的回答~
作者: xjxuvup 時間: 2013-8-12 14:58
我new一個Asynctask,然後在doInBackground裡面
可以用Class名稱.this.getWindowManager().getDefaultDisplay()
不會出錯
所以應該可以解決你的問題
作者: cchahaliu 時間: 2013-8-12 15:04
xjxuvup 發表於 2013-8-12 14:58
我new一個Asynctask,然後在doInBackground裡面
可以用Class名稱.this.getWindowManager().getDefaultDispl ...
請問是類似這樣的程式碼嗎?- public class RequestTask extends AsyncTask<String, String, String>{
- @Override
-
- protected String doInBackground(String... uri) {
-
- RequestTask.this.getWindowManager().getDefaultDisplay();
- ............
-
複製代碼 可是他還是顯示
"The method getWindowManager() is undefined for the type RequestTask."
!!
作者: xjxuvup 時間: 2013-8-12 15:41
大概是像下圖這樣