This article is an example of the Android development of contact modification, new contact method, through this example code can implement add contact, edit modify contact, add new contacts and update contacts, and other operations, operations mainly in the thread processing, and in the process of operation, display the circular progress bar, In the Android system, this is a more common kind of progress bar style.
The specific functional code looks like this:
Package huahua.contactsfragment;
Import java.util.Collections;
Import HUAHUA.HUAHUACONTACTS.R;
Import Huahua.huahuacontacts.Utils;
Import android.app.Activity;
Import Android.app.ProgressDialog;
Import Android.content.ContentResolver;
Import Android.content.ContentUris;
Import android.content.ContentValues;
Import android.content.Intent;
Import Android.net.Uri;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import Android.provider.ContactsContract;
Import Android.provider.ContactsContract.CommonDataKinds.Phone;
Import Android.provider.ContactsContract.CommonDataKinds.StructuredName;
Import android.provider.ContactsContract.RawContacts;
Import Android.provider.ContactsContract.RawContacts.Data;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.Window;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Addcontactsactivity extends activity{private Button m_savebtn; Private EditText M_editname;
Private EditText M_editnum;
Private TextView M_texttitle;
Private String M_contactid;
private int m_type;
ProgressDialog m_dialogloading;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
This.requestwindowfeature (Window.feature_no_title);
Setcontentview (r.layout.add_contacts);
Intent Intent = Getintent ();
Bundle Bundle = Intent.getbundleextra ("person");
M_texttitle = (TextView) Findviewbyid (r.id.text_title);
M_editname = (edittext) Findviewbyid (r.id.edit_name);
M_editnum = (edittext) Findviewbyid (r.id.edit_num);
M_type = Bundle.getint ("Tpye");
M_editname.settext (bundle.getstring ("name"));
M_editnum.settext (bundle.getstring ("number"));
if (m_type = 0)//new Contact {m_texttitle.settext ("new Contact");
else if (M_type = 1)//Edit Contact {M_contactid = bundle.getstring ("id");
M_texttitle.settext ("Edit Contact");
} m_savebtn = (Button) Findviewbyid (r.id.btn_save_contact); M_savebtn.setonclicklistener (New View.onclIcklistener () {@Override public void OnClick (View v) {if ("". Equals (M_editname.gettext (). toString ())) {Toast.
Maketext (Addcontactsactivity.this, "Please enter contact name", Toast.length_short). Show (); else if ("". Equals (M_editnum.gettext (). toString ())) {Toast.maketext (addcontactsactivity.this, enter contact phone, TOAST.L
Ength_short). Show ();
else if (M_type = 0) {//New contact action, place process new Savecontacttask () in Thread. Execute ();
else if (M_type = 1) {//Update the contact action, place the new Changecontacttask () in the thread. Execute ();
}
}
}); Class Savecontacttask extends Asynctask<void, Integer, void>{@Override protected void doinbackground (void ... p
Arams) {utils.addcontact (M_editname.gettext (). ToString (), M_editnum.gettext (). ToString ());
return null;
} @Override protected void onpostexecute (void result) {if (m_dialogloading!= null) {M_dialogloading.dismiss ();
Finish (); }} @Override protected void OnPreExecute () {m_dialogloading = new ProgressDialog (AddcontactsaCtivity.this); M_dialogloading.setprogressstyle (Progressdialog.style_spinner)//Set style to Circle progress bar m_dialogloading.setmessage ("Saving contacts
");
M_dialogloading.setcancelable (FALSE);
M_dialogloading.show (); @Override protected void Onprogressupdate (Integer ... values) {}] class Changecontacttask extends Asynctask<voi D, Integer, void>{@Override protected void doinbackground (void ... params) {utils.changecontact (m_editname.gettext
(). ToString (), M_editnum.gettext (). ToString (), M_contactid);
return null;
} @Override protected void onpostexecute (void result) {if (m_dialogloading!= null) {M_dialogloading.dismiss ();
Finish ();
}} @Override protected void OnPreExecute () {m_dialogloading = new ProgressDialog (addcontactsactivity.this); M_dialogloading.setprogressstyle (Progressdialog.style_spinner)//Set style to Circle progress bar m_dialogloading.setmessage ("Saving contacts
");
M_dialogloading.setcancelable (FALSE);
M_dialogloading.show (); } @Override Protected void Onprogressupdate (Integer ... values) {}}}