我在实现Asynctask方面遇到了问题。
我想显示一个微调器进度条并同时在后台执行操作。
到目前为止,这是我的代码。
你能告诉我我做错了吗?
在运行代码时,progregressbar运行正常,但随后应用程序停止工作!
请帮忙。
私有类Writeinbg扩展了AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... urls) {
// Check external media availability.
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String sstate = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(sstate)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
File dir = new File (root.getAbsolutePath() + "/download");
dir.mkdirs();
File file = new File(dir, "phonecontacts.txt");
tv.setText("");
tv.append("\nThe following contacts have been written to file:\n");
try{
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
ContentResolver cr = getContentResolver();
Cursor cu = cr.query(URI, null, null, null, null);
if (cu.getCount() > 0) {
// Loop over all contacts
while (cu.moveToNext()) {
// Initialize storage variables for the new contact
street = "";
city = "";
state = "";
postcode = "";
country = "";
// Get ID information (id, name and lookup key) for this contact. id is an identifier
// number, name is the name associated with this row in the database, and
// lookupKey is an opaque value that contains hints on how to find the contact
// if its row id changed as a result of a sync or aggregation.
id = cu.getString(cu.getColumnIndex(ID));
name = cu.getString(cu.getColumnIndex(DNAME));
lookupKey = cu.getString(cu.getColumnIndex(LOOKY));
// Append list of contacts to the scrollable TextView on the screen.
tv.append("\n..... " +name);
// Query phone numbers for this contact (may be more than one), so use a
// while-loop to move the cursor to the next row until moveToNext() returns
// false, indicating no more rows. Store the results in arrays since there may
// be more than one phone number stored per contact. The if-statement
// enclosing everything ensures that the contact has at least one phone
// number stored in the Contacts database.
phcounter = 0;
if (Integer.parseInt(cu.getString(cu.getColumnIndex(HPN))) > 0) {
Cursor pCur = cr.query(PURI, null, CID + " = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
ph[phcounter] = pCur.getString(pCur.getColumnIndex(PNUM));
phType[phcounter] = pCur.getString(pCur.getColumnIndex(PHONETYPE));
phcounter ++;
}
pCur.close();
}
// Query email addresses for this contact (may be more than one), so use a
// while-loop to move the cursor to the next row until moveToNext() returns
// false, indicating no more rows. Store the results in arrays since there may
// be more than one email address stored per contact.
emcounter = 0;
Cursor emailCur = cr.query(EURI, null, EID + " = ?", new String[]{id}, null);
while (emailCur.moveToNext()) {
em[emcounter] = emailCur.getString(emailCur.getColumnIndex(EMAIL));
emType[emcounter] = emailCur.getString(emailCur.getColumnIndex(EMAILTYPE));
emcounter ++;
}
emailCur.close();
// Query Address (assume only one address stored for simplicity). If there is
// more than one address we loop through all with the while-loop but keep
// only the last one.
addcounter = 0;
Cursor addCur = cr.query(AURI, null, AID + " = ?", new String[]{id}, null);
while (addCur.moveToNext()) {
street = addCur.getString(addCur.getColumnIndex(STREET));
city = addCur.getString(addCur.getColumnIndex(CITY));
state = addCur.getString(addCur.getColumnIndex(STATE));
postcode = addCur.getString(addCur.getColumnIndex(POSTCODE));
country = addCur.getString(addCur.getColumnIndex(COUNTRY));
addcounter ++;
}
addCur.close();
// Write identifiers for this contact to the SD card file
pw.println("\n Name:" +name +"\n");
// Write list of phone numbers for this contact to SD card file
for(int i=0; i<phcounter; i++){
pw.println("\n Phone Number"+"("+ getPhoneType(phType[i])+")" + ":"+ ph[i] +"\n");
}
// Write list of email addresses for this contact to SD card file
for(int i=0; i<emcounter; i++){
pw.println(" Email Address" + "(" + getEmailType(emType[i]) + "):" + em[i] +"\n");
}
// If street address is stored for contact, write it to SD card file
if(addcounter > 0){
pw.println("\n Adress:" +"\n");
if(street != null) pw.println(" \n\t Street:"+street +"\n");
if(city != null) pw.println(" \n\t City:"+city +"\n");
if(state != null) pw.println(" \t State/region:"+state +"\n");
if(postcode != null) pw.println(" \t Postcode:"+postcode +"\n");
if(country != null) pw.println(" \n Country"+country +"\n");
}
}
}
// Flush the PrintWriter to ensure everything pending is output before closing
pw.flush();
pw.close();
f.close();
Toast.makeText(getBaseContext(),"Writing complete!",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i("MEDIA", "*************** File not found. Did you" +
" add a WRITE_EXTERNAL_STORAGE permission to the manifest file? ");
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String post) {
dismissDialog(progress_bar_type);
Toast.makeText(getBaseContext(),"Task Completed",Toast.LENGTH_LONG).show();
}
}
然后我这样称呼它:
new Writeinbg()。execute(new String [] {“root.getAbsolutePath()”+“/ download”+“/ phonecontacts.txt”});