十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
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(); } } }; }