0

我是安卓新手。我试图从一个活动中得到这个:

i.putExtra("mlat", String.valueOf(latitude));

到这里:

public class PostDataActivity extends AsyncTask<Void, Void, Void>{
    GpsData gps;
//  double latitude = gps.getLatitude(); 
    //String mlat;

    @Override
    protected Void doInBackground(Void... params) {
        Bundle extras = getIntent().getExtras();
        String mlat = extras.getString("mlat");

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://test.de");
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
          nameValuePairs.add(new BasicNameValuePair("lat", mlat));
          nameValuePairs.add(new BasicNameValuePair("lon", "7.358154"));
           try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
           }
           try {
            httpClient.execute(httpPost);
           } catch (ClientProtocolException e) {
            e.printStackTrace();
           } catch (IOException e) {
            e.printStackTrace();
           }
        return null;
    }

但是 getIntent 是未定义的

Bundle extras = getIntent().getExtras();
String mlat = extras.getString("mlat");

如何在 Activity 和 doInBackground 之间传输数据?

4

3 回答 3

2

execute方法接受一个字符串参数数组,这些AsyncTask参数可用于将额外数据获取到后台任务中。

这是来自Android 源文档的示例:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

  ..
}

您可以通过这种方式从主线程调用它:

new DownloadFilesTask().execute(url1, url2, url3);
于 2012-10-22T12:50:11.750 回答
0

首先通过我的 Matt 提供的教程。

对于您的情况,您必须修改您的代码,如下所示:

1.将参数类型从 void 更改为 String:

公共类 PostDataActivity 扩展 AsyncTask< String , Void, Void>{

2.将您的数据作为参数传递给 AsyncTask:

新 PostDataActivity().execute(" mlat ");

于 2012-10-22T12:59:00.470 回答
0

Intent 不包含任何内容,因为您正在扩展AsyncTask. 我认为整个方法是有缺陷的。

由于PostDataActivity它是一个 Activity,它不应该扩展 AsyncTask。以下是我认为应该是什么样子的想法:

public class PostDataActivity extends Activity {
GPSData gps;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.);

    Bundle extras = getIntent().getExtras();
    String mlat = extras.getString("mlat");

    MyDownloaderTask task = new MyDownloaderTask();
    task.execute(mlat);
}
    //Check docs for parameterisation of AsyncTask
private class MyDownloaderTask extends AsyncTask<String, Void, String> {
    protected String doInBackground(String params...) {
        string someResult;
        String mlat = params[0];
        //Got your variable, do whatever you want from here on
        return someResult; //Since it is no longer AsyncTask<Void, Void, Void>
    }
    protected void onPostExecute(String result) {
        //This executes after task completes successfully
    }
    protected void onCancelled(String result) {
        //This executes after task completes unsuccessfully
    }
}

//...
}
于 2012-10-22T13:05:37.927 回答