我有一个活动,它首先是一个实施的过程对话框的AsyncTask的。这工作正常!但我希望得到一个字符串返回时AsyncTask的完成。所以,我必须返回的东西在onPostExecute - 方法。 这一结果(字符串)我要抢在活动,开始了AsyncTask的。 我不希望使用获得(),因为它会阻止UI线程。
我有什么写进去onPostExecute和活动抓住从doInBackground字符串?
感谢您的任何形式的帮助来解决这个问题;)
现在与code:
类BgDL扩展的AsyncTask<字符串,整数,字符串> {
字符串finishString =;
私人上下文的背景下;
ProgressDialog pdialog;
公共BgDL(上下文CXT){//获取上下文(通常是本,从活动/否则progressdialog不会显示出来!
上下文= CXT;
pdialog =新ProgressDialog(上下文);
}
@覆盖
保护字符串doInBackground(字符串...字符串){
OutputStream的输出;
ByteArrayOutputStream BAOS = NULL;
尝试 {
网址URL =新的URL(字符串[0]);
URLConnection的连接= url.openConnection();
connection.connect();
INT文件长度= connection.getContentLength();
的InputStream输入=新的BufferedInputStream(url.openStream());
如果(字符串[1] ==的toString){//写入字节字符串如果文件未给出
BAOS =新ByteArrayOutputStream();
输出=新DataOutputStream类(BAOS);
}其他{//否则串
输出=新的FileOutputStream(字符串[1]);
}
字节的数据[] =新的字节[1024];
总长= 0;
诠释计数;
而((计数= input.read(数据))!= - 1){
共有+ =计数;
publishProgress((int)的(总* 100 /文件长度));
output.write(数据,0,计数);
}
output.flush();
output.close();
input.close();
如果(字符串[1] ==的toString){
finishString = baos.toString(); //
} //只写字节字符串,如果文件没有给出
}赶上(例外五){log.d(恩,e.toString());
}
返回finishString;
}
@覆盖
在preExecute保护无效(){
super.on preExecute();
pdialog.setTitle(请稍候);
pdialog.setIndeterminate(假);
pdialog.setMax(100);
pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pdialog.show();
}
@覆盖
保护无效onProgressUpdate(整数...进度){
super.onProgressUpdate(进度);
pdialog.setProgress(进展[0]);
}
保护无效onPostExecute(字符串... finishString){
pdialog.dismiss(); // !!!!!!!!! finishString我要过到我的活动,其中初一的AsyncTask与.execute();
}
解决方案
创建一个类的项目,如下图所示延伸活动:
公共类SomeClass的扩展活动
{
公共无效dataFromPostExecute(字符串数据)
{
的System.out.println(在mainactivity);
}
}
如果你想为每一个活动的一个线程,只要创建一个扩展的类 应用程序
公共类异步扩展应用
{
私人插槽globalSocket;
@覆盖
公共无效的onCreate()
{
//插座= NULL;
}
公共插座getglobalSocket()
{
返回globalSocket;
}
公共无效setGlobalSocket(插座globalSocket)
{
this.globalSocket = globalSocket;
}
}
在延伸AsyncTask的执行以下操作的套接字类:
公共SocketClass延伸的AsyncTask<字符串,字符串,字符串>
{
异步应用程序;
私人SomeClass的t_act;
公共SocketClass(SomeClass的SCT)
{
t_act = SCT;
this.con = TST;
应用=((异步)sct.getApplicationContext());
}
保护无效onPostExecute(字符串数据)
{
t_act.dataFromPostExecute(数据);
}
}
然后,在你的活动延长SomeClass的和做的,如下所示:
公共类活性1扩展SomeClass的
{
公共无效dataFromPostExecute(字符串数据)
{
//做你想做的。这种方法的数据中包含从值
postexecute()
}
}
I have an Activity, which starts an AsyncTask with an Implemented process dialog. That works fine! But i want to get a String return when the asyncTask has finished. So i have to return something in the onPostExecute - Method. That result(string) i want to grab in the Activity, which started the AsyncTask. I do not want to use .get() because it blocks the UI thread.
What do i have to write into onPostExecute and the Activity grab the string from doInBackground?
Thank you for any kind of help to solve this problem ;)
Now with Code:
class BgDL extends AsyncTask<String, Integer, String> {
String finishString="";
private Context context;
ProgressDialog pdialog;
public BgDL(Context cxt) { //get the context (usually "this" from Activity / otherwise progressdialog wont show up!
context = cxt;
pdialog = new ProgressDialog(context);
}
@Override
protected String doInBackground(String... strings) {
OutputStream output;
ByteArrayOutputStream baos = null;
try {
URL url = new URL(strings[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
if (strings[1]=="toString") { // write byte to string if a file is not given
baos= new ByteArrayOutputStream();
output = new DataOutputStream(baos);
} else { //otherwise to string
output = new FileOutputStream(strings[1]);
}
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
if (strings[1]=="toString") {
finishString = baos.toString(); //
} // only write byte to string if a file is not given
} catch (Exception e) {log.d("ex",e.toString());
}
return finishString;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pdialog.setTitle("Please wait");
pdialog.setIndeterminate(false);
pdialog.setMax(100);
pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pdialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
pdialog.setProgress(progress[0]);
}
protected void onPostExecute(String...finishString) {
pdialog.dismiss();//!!!!!!!!!finishString i want to pass over to my Activity, which started this asynctask with .execute();
}
解决方案
Create a class in your project which extends activity as shown below:
public class SomeClass extends Activity
{
public void dataFromPostExecute(String data)
{
System.out.println("in mainactivity");
}
}
If you want a single thread for every activity, just create a class which extends Application
public class Async extends Application
{
private Socket globalSocket;
@Override
public void onCreate()
{
//socket = null;
}
public Socket getglobalSocket()
{
return globalSocket;
}
public void setGlobalSocket(Socket globalSocket)
{
this.globalSocket = globalSocket;
}
}
In your socket class which extends Asynctask do the following:
public SocketClass extends AsyncTask<String,String,String>
{
Async app;
private SomeClass t_act;
public SocketClass(SomeClass sct)
{
t_act = sct;
this.con = tst;
app= ((Async)sct.getApplicationContext());
}
protected void onPostExecute(String data)
{
t_act.dataFromPostExecute(data);
}
}
Then, in your activity extend SomeClass and do as shown below:
public class Activity1 extends SomeClass
{
public void dataFromPostExecute(String data)
{
//do whatever you want. "data" of this method contains the values from
postexecute()
}
}
上一篇:透明的活动,与灰色的微弱的色调色调、微弱、灰色、透明
下一篇:Android的Activity.onWindowFocusChanged不会从内TabHost称为Activity、Android、TabHost、onWindowFocusChanged