我的问题
如何在等待接收短信时在android中显示进度条?
该对话框应满足以下条件
该对话框最多应关闭5分钟。
如果在5分钟之前收到短信,则该对话框应关闭。
我尝试了什么
public class GetSMS extends Activity {
...
...
private boolean progressDialogFlag;
private ProgressDialog progressDialog;
...
...
new SMSReceiver().execute();
}
public class SMSReceiver extends AsyncTask<Void, Integer, Void> {
protected void onPreExecute() {
// start a progressdialog box
super.onPreExecute();
progressDialogFlag = true;
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Waiting to Receive SMS...");
progressDialog.setCancelable(false);
progressDialog.show();
}
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
if (progressDialogFlag == true) {
progressDialog.setProgress(progress[0]);
}
if (progress[0] == 100 || progressDialogFlag == false) {
progressDialog.dismiss();
}
}
protected void onPostExecute(Void result) {
}
@Override
protected Void doInBackground(Void... params) {
int interval = 2;
int totalDuration = 5 * 60 * 1000; // 5 min
int sleepPeriod = (totalDuration / 100) * interval;
for (int i = 2; i <= 100; i += 2) {
publishProgress(i);
try {
Thread.sleep(sleepPeriod);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
}
smsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
progressdialog = false;
}
}
我有什么疑问
我认为在5分钟之前收到Sms时使用
progressDialogFlag
消除progressDialog
会引起问题。在UI和工作线程中使用变量
progressDialogFlag
会导致某种竞争状况。感谢帮助。
最佳答案
可以在启动progressDialog时尝试如下所示的操作
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(null!=progressDialog && progressDialog.isShowing()){
// try{
progressDialog.dismiss();
}
}
}, 5*60*1000);
但是需要更多检查...
关于android - 如何在等待5分钟的短信时在android中显示进度条?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11014653/