更新的用户界面异步Android中 [英] Update UI Asynchronously in Android

查看:90
本文介绍了更新的用户界面异步Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被称为每10秒,应该更新与web服务回复一个TextView一个Web服务调用(或至少表明每10秒敬酒消息)

但用户界面是没有得到所有更新。

请在下面找到了code。

 公共类MessagesRequestActivity延伸活动{
    / **第一次创建活动时调用。 * /
    字符串currentMsg =默认;
    @覆盖
    公共无效的onCreate(包savedInstanceState){

        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        //调用Web服务
        的getMessage();
    }
    公共无效的getMessage(){

        尝试
        {
        SoapObject请求=新SoapObject(http://tempuri.org/,的getMessage);

        SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = TRUE;
        envelope.setOutputSoapObject(要求);

        // Web方法调用
        HttpTransportSE androidHttpTransport =新HttpTransportSE(http://192.168.4.50/WebService.asmx);
        androidHttpTransport.call(http://tempuri.org/+的getMessage,信封);
        //得到响应
        SoapPrimitive响应=(SoapPrimitive)envelope.getResponse();

        //响应对象可以通过它的名称进行检索:result.getProperty(对象名);
        字符串消息=(字符串)response.toString();
        Toast.makeText(此,信息,Toast.LENGTH_LONG).show();

        }
        赶上(例外五)
        {
        e.printStackTrace();
        }
        尝试 {
            视频下载(10000);
        }赶上(InterruptedException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        }
}
 

解决方案

下面是一个例子的AsyncTask

 公共类TalkToServer扩展的AsyncTask<字符串,字符串,字符串> {
@覆盖
在preExecute保护无效(){
    super.on preExecute();
}

@覆盖
保护无效onProgressUpdate(字符串...值){
    super.onProgressUpdate(值);

}

@覆盖
保护字符串doInBackground(字符串... PARAMS){
//这里做你的工作
    返回的东西;
}

@覆盖
保护无效onPostExecute(字符串结果){
    super.onPostExecute(结果);
       //做一些数据在这里显示,或发送到mainactivity
}
 

然后,您可以通过调用访问

  TalksToServer的varName =新TalkToServer(); //传递参数,如果你需要的构造
varName.execute();
 

异步文档 进度对话框示例

您不想做网络的东西,或致电睡眠 UI 线程。如果它是一个内部类,那么你将有机会获得外部类的成员变量。否则,建立在的AsyncTask 类构造器来传递上下文,如果你想从更新onPostExecute 或其它方法besids doInBackground()

I have a webservice call which is called every 10 seconds and should update a TextView with the webservice reply(or atleast show a toast message every 10 seconds)

But the UI is not getting updated at all.

Please find the code below.

public class MessagesRequestActivity extends Activity  {
    /** Called when the activity is first created. */
    String currentMsg="Default";
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Calling the webservice
        getMessage();
    }
    public void getMessage(){

        try
        {
        SoapObject request = new SoapObject("http://tempuri.org/", "getMessage");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        //Web method call
        HttpTransportSE androidHttpTransport = new HttpTransportSE("http://192.168.4.50/WebService.asmx");
        androidHttpTransport.call("http://tempuri.org/"+ "getMessage", envelope);
        //get the response
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

        //the response object can be retrieved by its name: result.getProperty("objectName");
        String message = (String)response.toString();
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();

        }
        catch (Exception e)
        {
        e.printStackTrace();
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
}

解决方案

Here is an example of an AsyncTask

public class TalkToServer extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);

}

@Override
protected String doInBackground(String... params) {
//do your work here
    return something;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
       // do something with data here-display it or send to mainactivity
}

Then you can access by calling

TalksToServer varName = new TalkToServer(); //pass parameters if you need to the constructor
varName.execute();

Async Docs Progress Dialog Example

You don't want to do network stuff or call sleep on the UI thread. If it is an inner class then you will have access to your member variables of the outer class. Otherwise, create a contructor in the AsyncTask class to pass context if you want to update from onPostExecute or other methods besids doInBackground().

这篇关于更新的用户界面异步Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆