我需要将一个字符串变量从我的主activity类发送到api类,并使用该字符串作为url的一部分来进行AsyncTask调用。
我尝试使用Intent和share首选项,但似乎都不能在AsyncTask类中访问。我可以使用单例模式吗?如果可以,我该如何使用它?
发布于 2014-09-04 07:27:10
如果你声明了一个全局变量:
public class MainActivity extends Activity {
private String url = "http://url.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new DownloadFilesTask().execute();
}
private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
protected Long doInBackground(Void... params) {
// You can use your 'url' variable here
return null;
}
protected void onProgressUpdate(Void... result) {
return null;
}
protected void onPostExecute(Void result) {
}
}
}如果您在单独的类中工作:
new MyAsyncTask("Your_String").execute();private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
public MyAsyncTask(String url) {
super();
// do stuff
}
// doInBackground()
}https://stackoverflow.com/questions/25655071
复制相似问题