我有不确定的进度条,当我从互联网上读取数据时显示。为了显示/消失进度条,我使用它的可见性。当我打开进度条选项时,我的应用程序从网络读取数据15分钟,当我删除进度条代码时,它持续20秒。它有可能使我的应用程序变得如此缓慢?
main_activity.xml:
<TableRow
android:gravity="center_horizontal"
android:layout_marginTop="10dip"
android:visibility="gone"
android:id="@+id/progress_bar"
>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="30dip"
android:minWidth="30dip"
android:maxHeight="30dip"
android:minHeight="30dip"
android:indeterminate="true"
android:layout_gravity="center_horizontal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/searchingInProgress"
android:layout_gravity="center|center_horizontal"
/>
</TableRow>
第一个片段java代码(这通过执行类FindSongTask触发与net的连接,继承自ASyncTask):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button searchButton = (Button) findViewById(R.id.searchButton);
searchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText searchQueryField = (EditText) findViewById(R.id.searchQueryField);
String searchQuery = searchQueryField.getText().toString();
if (searchQuery.length() > 0 && (currentQuery == null || !currentQuery.equals(searchQuery))) {
new FindSongTask().execute(searchQuery);
currentQuery = searchQuery;
findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
v.setEnabled(false);
}
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchQueryField.getWindowToken(), 0);
}
});
}
第二个片段(这是在连接到网络后执行的):
instance.findViewById(R.id.searchButton).setEnabled(true);
findViewById(R.id.progress_bar).setVisibility(View.GONE);
super.onPostExecute(result);
答案 0 :(得分:0)
尝试这样的事情......
public class MyActivity extends Activity{
ProgressDialog pd;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
}
@Override
public void onPause()
{
//don't leak a window
super.onPause();
if(pd != null)
pd.dismiss();
}
//no networking allowed on main thread
private class NetworkingTask extends AsyncTask<String,String,String>{
int result;
@Override
protected void onPreExecute(){
pd = ProgressDialog.show(Login.this,"","Display message...", true,false);
}
@Override
protected String doInBackground(String...strings){
//perform networking here
return null;
}
@Override
protected void onPostExecute(String unused){
//check result
pd.dismiss();
Intent intent = new Intent(MyActivity.this,NextActivity.class);
startActivity(intent);
}
}
}