zoukankan      html  css  js  c++  java
  • AsyncTask

    package com.example.testinvokewebservice;
    
    import android.os.AsyncTask;
    import android.widget.TextView;
    
    import com.model.ParamsModel;
    public class DownloadAsyncTask extends AsyncTask<ParamsModel, Integer, String>{
    
        public TextView view;
        private int i=0;
        @Override
        protected String doInBackground(ParamsModel... params) {
            // TODO Auto-generated method stub
            i = params[0].getiStart();
            for(int i1=0;i1<100;i1++)
            {
                i++;
                try {
                    Thread.sleep(1000);
                    publishProgress(i);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
            return Integer.toString(i);
        }
    
        /* (non-Javadoc)
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            view.setText("正准备执行后台操作");
        }
    
        /* (non-Javadoc)
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            view.setText("执行后台操作完成,i的结果是:"+result);
        }
    
        /* (non-Javadoc)
         * @see android.os.AsyncTask#onProgressUpdate(java.lang.Object[])
         */
        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            //如果直接是  view.setText(values[0]) 会出错呢。。 这个地方很奇怪
            view.setText(values[0] + "");
        }
    }
    package com.example.testinvokewebservice;
    
    import com.model.ParamsModel;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            Button btn= (Button)findViewById(R.id.btnLogin);
            btn.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    EditText editText = (EditText)findViewById(R.id.editText);
                    String strValue = editText.getText().toString();
                    
                    TextView view = (TextView)findViewById(R.id.textView);
                    ParamsModel model = new ParamsModel();
                    model.setiStart(Integer.valueOf(strValue));
                    DownloadAsyncTask download = new DownloadAsyncTask();
                    download.view = view;
                    download.execute(model);
                }
            });
        }
    }
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.testinvokewebservice.MainActivity" >
    
        <EditText 
            android:id="@+id/editText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="请输入初始数字:"
            />
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/btnLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="195dp"
            android:gravity="center_vertical"
            android:text="调用WebService" />
    
    </RelativeLayout>
  • 相关阅读:
    get和post区别
    linux查看是否被入侵
    (转)MSSQL 各个发行版本版本号以及Compact 版本号
    (转)玩转-数据库行列转换
    (转)理解SQL SERVER中的分区表
    (转)使用CruiseControl+SVN+ANT实现持续集成之三
    (转)使用CruiseControl+SVN+ANT实现持续集成之二
    (转)使用SVN+CruiseControl+ANT实现持续集成之一
    (转)持续化集成工具CruiseControl.NET
    (转)DockPanel 右键增加关闭,除此之外全部关闭的功能
  • 原文地址:https://www.cnblogs.com/niuge/p/4577010.html
Copyright © 2011-2022 走看看