如何将值从Activity传递到同一个Activity中的任何位置?

时间:2014-01-10 14:44:02

标签: android

我正在进行下载项目。我想将活动中的字符串值传递给同一活动中的任何位置 我的请求:当我单击按钮时,String str传递给DownloadFromURL类并获取字符串
请帮帮我

代码:

public class FirstActivity extends Activity {

Button btnShow;
Intent i;


public static final int progress_bar_type = 0; 
private static String file_url = "http://api.androidhive.info/progressdialog/hive.jpg";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    btnShow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            String str = "file";//Value that I want to pass to DownloadFromURL class
            new DownloadFileFromURL().execute(file_url);
        }
    });
}


class DownloadFileFromURL extends AsyncTask<String, String, String> {



    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();

            int lenghtOfFile = conection.getContentLength();


            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            String res = "";//This should String that I want to get from FirstClass 
            OutputStream output = new FileOutputStream("/sdcard/"+res+".jpg");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                publishProgress(""+(int)((total*100)/lenghtOfFile));
                output.write(data, 0, count);
            }


            output.flush();


            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }

}

3 个答案:

答案 0 :(得分:2)

试试这个..在{strong> FirstActivity 中使用String str = ""作为全局变量,就像Button btnShow;一样

btnShow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            str = "file";//Value that I want to pass to DownloadFromURL class
            new DownloadFileFromURL().execute(file_url);
        }
    });

或者

   new DownloadFileFromURL(str).execute(file_url);

AsyncTask

  class DownloadFileFromURL extends AsyncTask<String, String, String> {

   String url;
    //  constructor
   public DownloadFileFromURL(String url) {
            this.url = url;
        }

         //as your code you can use url this the DownloadFileFromURL 

答案 1 :(得分:2)

new DownloadFileFromURL().execute(file_url, str);

并使用doInBackground方法:

URL url = new URL(f_url[0]);
String str = f_url[1];

答案 2 :(得分:0)

我认为你应该使用SharedPreferences,你也可以在其他活动中使用这些数据。

public void saveStageInstance(string data){
        SharedPreferences sharedPreferences = getSharedPreferences();
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("dataName", data);
        editor.commit();

    }
    public Boolean loadStageInstance() {
        SharedPreferences sharedPreferences = getSharedPreferences();
        return sharedPreferences.getBoolean("dataName", false);
    }