package com.example.testprogress; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ProgressDialog; @SuppressLint("HandlerLeak") public class MainActivity extends Activity { private ProgressDialog progressDialog = null; private boolean flag = true; private int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressDialog = new ProgressDialog(this); progressDialog.setMessage("数据获取中..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setCancelable(false);//是否可以被返回键取消 progressDialog.show(); for(int i=0; i<100; i++){ new Thread(mRunnable).start(); } } private Runnable mRunnable = new Runnable(){ @Override public void run() { while(flag){ try { Thread.sleep(1000); count ++; mHandler.sendMessage(mHandler.obtainMessage()); } catch (InterruptedException e) { e.printStackTrace(); } } } }; private Handler mHandler = new Handler(){ public void handleMessage(Message msg){ Log.v("article", Thread.currentThread().getId()+""); if(count >= 4){ flag = false; progressDialog.dismiss(); } } }; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。