Android 如何将值从活动传递到同一活动中的任意位置?
我正在处理一个下载项目。我想将活动中的字符串值传递到同一活动中的任何位置Android 如何将值从活动传递到同一活动中的任意位置?,android,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 =
我的请求:当我单击按钮时,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;
}
}
公共类FirstActivity扩展活动{
按钮显示;
意图一;
公共静态最终整数进度条类型=0;
私有静态字符串文件\u url=”http://api.androidhive.info/progressdialog/hive.jpg";
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShow.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
String str=“file”//我要传递给DownloadFromURL类的值
新建DownloadFileFromURL().execute(文件url);
}
});
}
类DownloadFileFromURL扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
试一试{
URL=新URL(f_URL[0]);
URLConnection conconnection=url.openConnection();
conconnect.connect();
int lenghtOfFile=conconnect.getContentLength();
InputStream输入=新的BufferedInputStream(url.openStream(),8192);
String res=“”;//这应该是我想从头等舱获取的字符串
OutputStream output=新文件OutputStream(“/sdcard/”+res+”.jpg”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
出版进度(“+(int)((总计*100)/长度文档));
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
}
试试这个..在FirstActivity中使用String str=“”
作为全局变量
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);
}
});
或
和异步任务
类
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
class DownloadFileFromURL扩展异步任务{
字符串url;
//建造师
公共下载FileFromURL(字符串url){
this.url=url;
}
//作为您的代码,您可以使用此url作为DownloadFileFromURL
试试这个..在FirstActivity中使用String str=“”
作为全局变量
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);
}
});
或
和异步任务
类
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
class DownloadFileFromURL扩展异步任务{
字符串url;
//建造师
公共下载FileFromURL(字符串url){
this.url=url;
}
//作为您的代码,您可以使用此url作为DownloadFileFromURL
在您的doInBackground
方法中:
URL url = new URL(f_url[0]);
String str = f_url[1];
在您的doInBackground
方法中:
URL url = new URL(f_url[0]);
String str = f_url[1];
我认为你应该使用SharedReferences,你也可以在其他活动中使用这些数据
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);
}
我认为你应该使用SharedReferences,你也可以在其他活动中使用这些数据
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);
}
如果
DownloadFileFromURL
是在活动之外声明的,那么这将不起作用scope@MarioStoilov但在本例中,它是在活动范围内定义的,因此work@MarioStoilov现在看看我的编辑,还有我的朋友。那会有用的。@tyczj是的,我只是指出了它,以防他决定在sep中提取这个类rate file我昨天尝试了这个,但它不起作用。但是现在它起作用了。谢谢。如果DownloadFileFromURL
在活动之外声明,它就不起作用了scope@MarioStoilov但在本例中,它是在活动范围内定义的,因此work@MarioStoilov现在看看我的编辑,还有我的朋友。那会有用的。@tyczj是的,我刚刚指出了这一点,以防他决定在一个单独的文件中提取这个类。我昨天尝试了这个方法,但现在不起作用。谢谢你如何在f_url[1]中添加我的字符串?看看。你将所有字符串传递给execute
方法,比如execute(str1,str2,str3…)您可以在doInBackground
方法中作为arry访问它们。如何在f_url[1]中添加我的字符串?请查看。您将所有字符串传递给execute
方法,如execute(str1、str2、str3…)您可以在doInBackground
方法中以arry的形式访问它们尽管这会起作用,但我不建议对频繁更改(如果他定期下载文件)、很大(文件可能是视频)或在不同线程中访问的数据使用sharedpref(异步使用共享pref时可能会产生奇怪的结果)。他说他只需要一个字符串,只在按下按钮时请求。虽然这会起作用,但我不建议对频繁更改(如果他定期下载文件)、较大(文件可能是视频)或在不同线程中访问的数据使用SharedReference(异步使用共享pref可能会产生奇怪的结果)。他说他只需要一个字符串,只在按下按钮时请求。