Android implements methods for adding and editing contacts

  • 2020-06-03 08:14:49
  • OfStack

Android instance introduced to modify the contact, and new contacts in the development of methods, through this example code which can realize adding contacts, edit contact, new contact and update the contact, such as operation, operating mainly in the thread processing, and in the process of operation, display circular progress bar in the Android system, it is a kind of more common progress bar style.

The specific function code is as follows:


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 contacts 
 {
  m_TextTitle.setText(" New contacts ");
 }
 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 the contact name ", Toast.LENGTH_SHORT).show();
  }
  else if("".equals(m_EditNum.getText().toString()))
  {
   Toast.makeText(AddContactsActivity.this, " Please enter the contact number ", Toast.LENGTH_SHORT).show();
  }
  else if(m_Type == 0)
  {
   // New contact operation , Put it in a thread for processing 
   new SaveContactTask().execute();
  }
  else if(m_Type == 1)
  {
   // Update contact operation , Put it in a thread for processing 
   new ChangeContactTask().execute();
  }
  }
 });
 }
 class SaveContactTask extends AsyncTask<Void, Integer, Void>{
 @Override
 protected Void doInBackground(Void... params) {
  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 the style to circular progress bar 
     m_dialogLoading.setMessage(" Saving contacts ");
     m_dialogLoading.setCancelable(false);
      m_dialogLoading.show();
 }
 @Override
 protected void onProgressUpdate(Integer... values) {
 }
 }
 class ChangeContactTask extends AsyncTask<Void, 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 the style to circular progress bar 
     m_dialogLoading.setMessage(" Saving contacts ");
     m_dialogLoading.setCancelable(false);
      m_dialogLoading.show();
 }
 @Override
 protected void onProgressUpdate(Integer... values) {
 }
 }
}

Related articles: