gpt4 book ai didi

android - 进度对话框 UI 卡住/缓慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:11:18 26 4
gpt4 key购买 nike

我有一个进度对话框,用于我程序的一部分,我在后台执行时间密集型操作,但是当显示对话框时,UI 或微调器图标卡住/缓慢/犹豫,使程序看起来好像它卡住。在我的 AsyncTaskonPostExecute 中,我关闭了对话框。

既然我在后台完成所有工作,为什么会发生这种情况?

这是我的代码

pDialog = ProgressDialog.show(FullGame.this,"Starting New Game","Please Wait...", true);
new StartNewGame().execute();

private class StartNewGame extends AsyncTask<Void,Void,Boolean>{

@Override
protected Boolean doInBackground(Void... params) {
try{
ContentValues values = new ContentValues();
Cursor c = getContentResolver().query(Games.PART1_URI,new String[] {Games.PART1_NUM},
Games.PART1_GAME_ID+"="+gameID+" AND "+Games.PART1_FRAME_NUM+"="+10,null,null);
c.moveToFirst();
String num = c.getString(0);
int part1 =0;
if(num.equals("-")){
part1=0;
}else{
part1=Integer.parseInt(num);
}

c = getContentResolver().query(Games.PART2_URI,new String[] {Games.PART2_NUM},
Games.PART2_GAME_ID+"="+gameID+" AND "+Games.PART2_FRAME_NUM+"="+10,null,null);
c.moveToFirst();
int part2 = 0;
if(num.equals("-")){
part2=0;
}else{
part2=Integer.parseInt(num);
}

c = getContentResolver().query(Games.PART3_URI,new String[] {Games.PART3_NUM},
Games.PART3_GAME_ID+"="+gameID,null,null);
c.moveToFirst();
int part3 = 0;
if(num.equals("-")){
part3=0;
}else{
part3=Integer.parseInt(num);
}

if(part1 == 10){
values.clear();
values.put(Games.STRIKES_FRAME_NUM,10);
values.put(Games.STRIKES_BOWLER_ID,bowlerClickedID);
values.put(Games.STRIKES_GAME_ID,gameID);
getContentResolver().insert(Games.STRIKES_URI, values);
}
if(part2 == 10){
values.clear();
values.put(Games.STRIKES_FRAME_NUM,10);
values.put(Games.STRIKES_BOWLER_ID,bowlerClickedID);
values.put(Games.STRIKES_GAME_ID,gameID);
getContentResolver().insert(Games.STRIKES_URI, values);
}
if(((part2+part3) == 10) && !score.checkSpare(10)){
values.clear();
values.put(Games.SPARES_BOWLER_ID,bowlerClickedID);
values.put(Games.SPARES_FRAME_NUM,10);
values.put(Games.SPARES_GAME_ID,gameID);
getContentResolver().insert(Games.SPARES_URI, values);
}
if(part3 == 10){
values.clear();
values.put(Games.STRIKES_FRAME_NUM,10);
values.put(Games.STRIKES_BOWLER_ID,bowlerClickedID);
values.put(Games.STRIKES_GAME_ID,gameID);
getContentResolver().insert(Games.STRIKES_URI, values);
}
c.close();
}catch(Exception e){
Log.d("FullGame",e.toString());
}

Date date = new Date(System.currentTimeMillis());
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String newDate = df.format(date);

ContentValues values = new ContentValues();
values.put(Games.GAMES_BOWLER_ID,bowlerClickedID);
values.put(Games.GAMES_TEAM_ID,1);
values.put(Games.GAMES_DATE,newDate);
values.put(Games.GAME_SEASON, pref.getLong(Preferences.SELECTED_SEASON, 1));
values.put(Games.GAMES_TOURNAMENT_ID, tournamentID);
Uri uri = getContentResolver().insert(Games.GAMES_URI, values);
gameID = ContentUris.parseId(uri);
int gameid = Integer.valueOf(String.valueOf(gameID));
values.clear();
Cursor cName = getContentResolver().query(BowlersDB.CONTENT_URI,new String[] {BowlersDB.FIRST_NAME},BowlersDB.ID+"="+bowlerClickedID,null,null);
cName.moveToFirst();
String name = cName.getString(0);
for(int i = 0;i<10;i++){
int num = i+1;
values.put(Games.NAMES_FRAME_NUM,num);
values.put(Games.NAMES_GAME_ID,gameid);
values.put(Games.NAMES_NAME,name);
getContentResolver().insert(Games.NAMES_URI, values);
names(i,name);
values.clear();
values.put(Games.PART1_FRAME_NUM,num);
values.put(Games.PART1_NUM,"0");
values.put(Games.PART1_GAME_ID,gameid);
getContentResolver().insert(Games.PART1_URI, values);
values.clear();
values.put(Games.PART2_FRAME_NUM,num);
values.put(Games.PART2_NUM,"0");
values.put(Games.PART2_GAME_ID,gameid);
getContentResolver().insert(Games.PART2_URI, values);
values.clear();
values.put(Games.TOTALS_FRAME_NUM,num);
values.put(Games.TOTALS_FRAME_TOTAL,"0");
values.put(Games.TOTALS_GAME_ID,gameid);
getContentResolver().insert(Games.TOTALS_URI, values);
values.clear();
values.put(Games.POCKETS_BOWLER_ID,bowlerClickedID);
values.put(Games.POCKETS_FRAME_NUM,i);
values.put(Games.POCKETS_GAME_ID,gameID);
values.put(Games.POCKETS_TEAM_ID, teamSelectedID);
values.put(Games.POCKETS_TOURNAMENT_ID, tournamentID);
values.put(Games.POCKETS_NUM, 0);
values.put(Games.POCKETS_SEASON, pref.getLong(Preferences.SELECTED_SEASON, 1));
getContentResolver().insert(Games.POCKETS_URI, values);
values.clear();
}

values.put(Games.PART3_GAME_ID,gameid);
values.put(Games.PART3_NUM,"0");
getContentResolver().insert(Games.PART3_URI, values);
cName.close();
part1Array = new int[10];
part2Array = new int[10];
totalsArray = new int[10];
part3 = 0;
mPinsUp = new ArrayList<Long>();
mPinsUp.add((long) 1);
mPinsUp.add((long) 2);
mPinsUp.add((long) 3);
mPinsUp.add((long) 4);
mPinsUp.add((long) 5);
mPinsUp.add((long) 6);
mPinsUp.add((long) 7);
mPinsUp.add((long) 8);
mPinsUp.add((long) 9);
mPinsUp.add((long) 10);
return true;
}

protected void onPostExecute(Boolean result){
pDialog.dismiss();
}

}

更新:昨晚在 Debug模式下运行代码,它似乎开始在 for 循环中执行,但所有这些仍然是在一个单独的线程中完成的,我只是将值插入我的数据库

更新 2如果我注释掉 for 循环,进度对话框会显示不到一秒钟,所以即使我在 AsyncTask 中执行所有操作,插入内容仍必须在 UI 线程中运行

最佳答案

这可能是因为您的后台线程占用了 100% 的设备 CPU。当 CPU 忙于处理一个线程时,UI 线程将不会更新,因此您会看到它已卡住

尝试通过从 doInBackground 中删除部分代码并再次运行应用来检测什么是最激进的操作。还可以尝试查看当设备未通过 USB 插入时它的性能如何 - 这有时会产生奇怪的结果

关于android - 进度对话框 UI 卡住/缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11916098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com