zoukankan      html  css  js  c++  java
  • 异步任务AsyncTask

    TestAsync.java

    public class TestAsync extends Activity{
        ProgressBar progressBar;
        TextView textView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test_handler);
    
            progressBar=(ProgressBar)findViewById(R.id.progressBar);
    
            textView=(TextView)findViewById(R.id.handler_text);
            progressBar.setVisibility(View.INVISIBLE);
    
            Button sendButton=(Button)findViewById(R.id.handler_sendButton);
            sendButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MyAsyncTask asyncTask=new MyAsyncTask();
                    asyncTask.execute("1000");
                }
            });
    
        }
        private  class MyAsyncTask extends AsyncTask<String ,Integer,String>
        {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progressBar.setVisibility(View.VISIBLE);
            }
    
            @Override
            protected String doInBackground(String... strings) {
                String result="";
                int myProgress=0;
                int inputLength=100;
                for(int i=1;i<=inputLength;i++)
                {
                    myProgress=i;
                    result=result+"*";
                    try{
                        Thread.sleep(100);
                    }catch (InterruptedException e){}
                    publishProgress(myProgress);
                }
                //返回一个值,将它返回给onPostExecute
                return result;
            }
    
            @Override
            protected void onProgressUpdate(Integer... values) {
                //和UI线程同步,可以在这里对UII界面更新
                //例如:更新进度条,Notification或者其他UI元素
                super.onProgressUpdate(values);
                progressBar.setProgress(values[0]);
    
            }
            @Override
            protected void onPostExecute(String s) {
                //和UI线程同步
                //更行UI Dialog 或者Notifiaction报告结果
                super.onPostExecute(s);
                Toast.makeText(TestAsync.this,"Success",Toast.LENGTH_SHORT).show();
                textView.setText(s);
                progressBar.setVisibility(View.GONE);
            }
        }
    }

    activity_test_handle.xml

    <LinearLayout 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:orientation="vertical"
       >
        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar"
            android:layout_width="match_parent" />
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:orientation="horizontal">
               <EditText
                   android:inputType="number"
                   android:hint="a:"
                   android:id="@+id/handler_parText1"
                   android:layout_width="0dp"
                   android:layout_weight="1"
                   android:layout_height="wrap_content"
                   />
    
    
    
               <EditText
                   android:inputType="number"
                   android:hint="b:"
                   android:id="@+id/handler_parText2"
                   android:layout_weight="1"
                   android:layout_width="0dp"
                   android:layout_height="wrap_content"
                 />
           </LinearLayout>
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/handler_sendButton"
                android:text="send"
                android:background="@drawable/send_button_selector"
                android:layout_width="match_parent"
                android:textColor="#ffffff"
                android:textSize="30sp"
                android:layout_height="wrap_content"/>
    </TableRow>
    
        <TextView
            android:id="@+id/handler_text"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="25sp"
            />
    </LinearLayout>


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    IE兼容问题,各类css hack代码(亲测有效)
    javascript进阶系列专题:闭包(Closure)
    javascript进阶系列专题:作用域与作用域链
    github文件上传及github pages博客搭建教程
    javascript算法
    【CSS3】标签使用说明
    【html5】常见标签使用说明(持续更新)
    通过一行代码学习javascript
    Object.create()兼容实现方法
    oracle查询锁表解锁语句 (转)
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768459.html
Copyright © 2011-2022 走看看