1

嗨,我有以下代码用于从 Andorid 手机读取联系人并将它们写入文件 - 我试图显示以下过程的进度是用于读取和写入联系人的代码

package com.lightcone.readcontacts;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;


public class ReadContacts extends Activity {

    // To suppress notational clutter and make structure clearer, define some shorthand constants.

    private static final Uri URI = ContactsContract.Contacts.CONTENT_URI;
    private static final Uri PURI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    private static final String ID = ContactsContract.Contacts._ID;
    private static final String DNAME = ContactsContract.Contacts.DISPLAY_NAME;
    private static final String HPN = ContactsContract.Contacts.HAS_PHONE_NUMBER;
    private static final String CID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
    private static final String PNUM = ContactsContract.CommonDataKinds.Phone.NUMBER;
    private static final String PHONETYPE = ContactsContract.CommonDataKinds.Phone.TYPE;
    private String id;
    private String name;
    private String ph[];
    private String phType[];
    private File root;
    private int phcounter;
    private TextView tv;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);

        // Allow for up to 9 email and phone entries for a contact
        ph = new String[9];
        phType = new String[9];

        // Check that external media available and writable
        checkExternalMedia();

        ContentResolver ctr = getContentResolver();
        Cursor cur = ctr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        int numberOfContacts = cur.getCount();
        Toast.makeText(this, "Reading" +String.valueOf(numberOfContacts)+ "Contacts", Toast.LENGTH_LONG).show();

        File dir = new File (root.getAbsolutePath() + "/download");
        dir.mkdirs();
        File file = new File(dir, "phoneData.txt");
        tv.append("Wrote " +numberOfContacts+" to "+file+"\nfor following contacts:\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()) {   

                    id = cu.getString(cu.getColumnIndex(ID));
                    name = cu.getString(cu.getColumnIndex(DNAME));
                    tv.append("\n"+id+" "+name); 

                    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();
                    }

                    // Write identifiers for this contact to the SD card file
                    pw.println("<nc>"+name);
                    for(int i=0; i<phcounter; i++){
                        pw.println("\tphone="+ ph[i]);
                    }
                }
            }       
            // Flush the PrintWriter to ensure everything pending is output before closing
            pw.flush();
            pw.close();
            f.close();       
        } 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();
        }   
    }   

    private void checkExternalMedia () {
        boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // We can only read the media
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            // Can't read or write
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }

        root = android.os.Environment.getExternalStorageDirectory(); 
        tv.append( "External storage: Exists="+mExternalStorageAvailable+", Writable="
            +mExternalStorageWriteable+" Root="+root+"\n");
    }

    private String getPhoneType(String index){
         if(index.trim().equals( "1")){
             return "home";
         } else if (index.trim().equals("2")){
             return "mobile";
         } else if (index.trim().equals("3")){
             return "work";
         } else if (index.trim().equals("7")){
             return "other";
         } else {
             return "?";
         }
    }  
}
4

1 回答 1

0

使用AsyncTask将联系人写入文件并在 AsyncTask 的 onPreExecute() 方法中显示进度条,并在AsyncTaskonPostExecute ()方法中关闭进度。

于 2013-02-22T13:01:44.543 回答