سؤال

For some reason, I want to get user's phone information in my asynctask function.

here is my asynctask code

package com.example.myfirstapp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.DisplayMetrics;
import android.util.Log;
public class RequestTask extends AsyncTask<String, String, String>{
@Override

protected String doInBackground(String... uri) {
    ...my code
}



@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    //Do anything with response..
}

private  String getWeith(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;
}  
}

error message show that "The method getWindowManager() is undefined for the type RequestTask."

but I've try to import everything I know... And this "getWeith()" function can work on my mainactivity. Please help me, thanks.

هل كانت مفيدة؟

المحلول

Reason for error

getWindowManager() is a method on Activity not on an AsyncTask so you will get the error "The method getWindowManager() is undefined for the type RequestTask.".

How to fix it?

In order to get this method inside AsyncTask use getSystemService() to retrieve a WindowManager then do you task.

See this documentation of WindowManager

نصائح أخرى

You're extending AsyncTask, which doesn't have the method getWindowManager(). You need access to the object for which this method is defined.

So, try passing your activity to your RequestTask object, and calling getWindowManager() on that activity.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top