ProgressDialog Android
我正在尝试使用ProgressDialog。当我运行我的应用程序时,进度对话框会在1秒后显示并消失。我想在完成我的流程时展示它。。这是我的密码:ProgressDialog Android,android,progressdialog,Android,Progressdialog,我正在尝试使用ProgressDialog。当我运行我的应用程序时,进度对话框会在1秒后显示并消失。我想在完成我的流程时展示它。。这是我的密码: public class MainActivity extends Activity { android.view.View.OnClickListener mSearchListenerListener; private ProgressDialog dialog; @Override public void onCreate(Bundl
public class MainActivity extends Activity {
android.view.View.OnClickListener mSearchListenerListener;
private ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new YourCustomAsyncTask().execute(new String[] {null, null});
}
private class YourCustomAsyncTask extends AsyncTask <String, Void, Void> {
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading....");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show(); //Maybe you should call it in ruinOnUIThread in doInBackGround as suggested from a previous answer
}
protected void doInBackground(String strings) {
try {
// search(strings[0], string[1]);
runOnUiThread(new Runnable() {
public void run() {
// updateMapWithResult(); //Or call it onPostExecute before progressDialog's dismiss. I believe this method updates the UI so it should run on UI thread
}
});
} catch(Exception e) {
}
}
@Override
protected void onPostExecute(Void params) {
dialog.dismiss();
//result
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
}
}
公共类MainActivity扩展活动{
android.view.view.OnClickListener mSearchListenerListener;
私人对话;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建YourCustomAsyncTask().execute(新字符串[]{null,null});
}
私有类YourCustomAsyncTask扩展了AsyncTask{
受保护的void onPreExecute(){
dialog=新建ProgressDialog(MainActivity.this);
setMessage(“加载…”);
对话框。setUndeterminate(true);
对话框。可设置可取消(true);
dialog.show();//也许您应该在ruinonithread in doInBackGround中调用它,正如前面的答案所建议的那样
}
受保护的void doInBackground(字符串){
试试{
//搜索(字符串[0],字符串[1]);
runOnUiThread(新的Runnable(){
public void run(){
//updateMapWithResult();//或者在progressDialog关闭之前调用它onPostExecute。我相信这个方法会更新UI,所以它应该在UI线程上运行
}
});
}捕获(例外e){
}
}
@凌驾
受保护的void onPostExecute(void参数){
dialog.dismise();
//结果
}
@凌驾
受保护的Void doInBackground(字符串…参数){
//TODO自动生成的方法存根
返回null;
}
}
}
更新问题:
@Override
public void onCreate(SQLiteDatabase db) {
mDatabase = db;
Log.i("PATH",""+mDatabase.getPath());
mDatabase.execSQL(FTS_TABLE_CREATE);
loadDictionary();
}
/**
* Starts a thread to load the database table with words
*/
private void loadDictionary() {
new Thread(new Runnable() {
public void run() {
try {
loadWords();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
private void loadWords() throws IOException {
Log.d(TAG, "Loading words...");
for(int i=0;i<=25;i++)
{ //***//
final Resources resources = mHelperContext.getResources();
InputStream inputStream = resources.openRawResource(raw_textFiles[i]);
//InputStream inputStream = resources.openRawResource(R.raw.definitions);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
StringBuilder sb = new StringBuilder();
while ((word = reader.readLine()) != null)
{
sb.append(word);
// Log.i("WORD in Parser", ""+word);
}
String contents = sb.toString();
StringTokenizer st = new StringTokenizer(contents, "||");
while (st.hasMoreElements()) {
String row = st.nextElement().toString();
String title = row.substring(0, row.indexOf("$$$"));
String desc = row.substring(row.indexOf("$$$") + 3);
// Log.i("Strings in Database",""+title+""+desc);
long id = addWord(title,desc);
if (id < 0) {
Log.e(TAG, "unable to add word: " + title);
}
}
} finally {
reader.close();
}
}
Log.d(TAG, "DONE loading words.");
}
@覆盖
public void onCreate(SQLiteDatabase db){
mDatabase=db;
Log.i(“PATH”,“+mDatabase.getPath());
mDatabase.execSQL(FTS_TABLE_CREATE);
loadDictionary();
}
/**
*启动一个线程以加载包含单词的数据库表
*/
私有void加载字典(){
新线程(newrunnable()){
公开募捐{
试一试{
loadWords();
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
}).start();
}
私有void loadWords()引发IOException{
Log.d(标签“加载单词…”);
对于(inti=0;i你不能有这个
runOnUiThread(new Runnable() {
public void run() {
// updateMapWithResult(); //Or call it onPostExecute before progressDialog's dismiss. I believe this method updates the UI so it should run on UI thread
}
});
在您的doInBackground()中
当在主UI线程上执行某些其他操作时,“进度”对话框不具有优先权。只有在后台执行这些操作时,才会使用“进度”对话框。在doInBackground中运行“进度”对话框不会对您有所帮助。这是progressdialog仅在几秒钟内可见的正常行为。您有两个doInBackground()
方法在AsyncTask
类中。从第一个doInBackground()
中删除runOnUiThread()
,并将其移动到第二个doInBackground()
中,其中包含@Override
注释
我不知道你是想写两个doInBackground()
方法还是错了,但是在方法之间有这样的混淆是不好的。你的AsyncTask
没有调用第一个doInBackground()
,它将调用doInBackground()
具有@Override
注释。因此您的进度对话框将在1秒内被取消,因为它立即返回null。我也遇到过同样的情况,尚未找到任何解决方案。很抱歉。@Akhter:为什么不调用updateMapWithResult();inside onPostExecute?@ρ∑ρєK我想他想在视图完全绘制之前显示一个进度对话框。所以他尝试了这种方法。@AndroSelva:是的,但我们可以调用dialog.discouse();after updateMapWithResult();inside方法onPostExecute@AndroSelva:oh!,表示内部updateMapWithResult()方法Op正在执行繁重的任务,然后Op必须更改代码以在doInBackground中运行繁重的任务,然后使用结果更新Ui。Thanksi这样做了,但对话框仅在1毫秒内消失