Monday, June 4, 2012

Re: [android-developers] need to remove contact from group ..

code for delete all contacts of phonebook might be you get some idea !!!!
import java.util.Vector;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class deleteall extends Activity {
    String TAG = "Delete1.5";
    ContentResolver cr = null;
Activity m_activity = this;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main7);
    cr = getContentResolver();
    Button button = null;
    button = (Button)findViewById(R.id.Button01);//delete button
    if(button != null){
            button.setOnClickListener(new View.OnClickListener(){

                            @Override
                            public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    CheckBox checkbox = null;
                                    checkbox = (CheckBox)findViewById(R.id.CheckBox01);//contact checkbox
                                    if(checkbox != null){
                                            if(checkbox.isChecked()){
                                                    deleteAllContact();
                                            }
                                    }
                                    checkbox = (CheckBox)findViewById(R.id.CheckBox02);//calendar checkbox
                                    if(checkbox != null){
                                            if(checkbox.isChecked()){
                                                    deleteAllCalendar();
                                            }
                                    }
                                    checkbox = (CheckBox)findViewById(R.id.CheckBox03);//sms checkbox
                                    if(checkbox != null){
                                            if(checkbox.isChecked()){
                                                    deleteAllSms();
                                            }
                                    }
                            }
            }
            );
    }
    button = (Button)findViewById(R.id.Button02);//exit button
    if(button != null){
            button.setOnClickListener(new View.OnClickListener(){

                            @Override
                            public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    m_activity.finish();
                            }
            }
            );
    }
    Log.i(TAG, "Out onCreate()");
}

void deleteAllContact(){
    Log.i(TAG, "In deleteAllContact()");
    Uri uri_contacts = Contacts.People.CONTENT_URI;
    String[] projection = {Contacts.People._ID};
    int columnIndex = 0;
    String str_id = "";
    Vector<String> vector_id = new Vector<String>();
    int delRow = 0;
    String where = "";
    try {
            Cursor cursor = cr.query(uri_contacts, projection, null, null, null);
            if(cursor.moveToFirst()){
                    do{
                            columnIndex = cursor.getColumnIndex(Contacts.People._ID);
                            str_id = cursor.getString(columnIndex);
                            vector_id.add(str_id);
                    }while(cursor.moveToNext());
            }
            cursor.close();
            for(int i=0; i<vector_id.size(); i++){
                    str_id = vector_id.get(i);
                    where = Contacts.People._ID+"="+str_id;
                    delRow = cr.delete(uri_contacts, where, null);
                    Log.i(TAG, "deleteAllContact(),delRow:"+delRow);
            }
            } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Log.e(TAG, "deleteAllContact(),Exception");
                    e.printStackTrace();
            }
            Log.i(TAG, "Out deleteAllContact()");
            Toast.makeText(this, "All Contacts from your mobile deleted sucessfully",
                    Toast.LENGTH_LONG).show();
}

void deleteAllCalendar(){
    Log.i(TAG, "In deleteAllCalendar()");
    String strUriEvents = "content://calendar/events";
    Uri uri_calendar = Uri.parse(strUriEvents);
    String str_column_name = "_id";
    String[] projection = {str_column_name};
    int columnIndex = 0;
    String str_id = "";
    Vector<String> vector_id = new Vector<String>();
    int delRow = 0;
    String where = "";
    try {
            Cursor cursor = cr.query(uri_calendar, projection, null, null, null);
            if(cursor.moveToFirst()){
                    do{
                            columnIndex = cursor.getColumnIndex(str_column_name);
                            str_id = cursor.getString(columnIndex);
                            vector_id.add(str_id);
                    }while(cursor.moveToNext());
            }
            cursor.close();
            for(int i=0; i<vector_id.size(); i++){
                    str_id = vector_id.get(i);
                    where = str_column_name+"="+str_id;
                    delRow = cr.delete(uri_calendar, where, null);
                    Log.i(TAG, "deleteAllCalendar(),delRow:"+delRow);
            }
            } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Log.e(TAG, "deleteAllCalendar(),Exception");
                    e.printStackTrace();
            }
            Log.i(TAG, "Out deleteAllCalendar()");
            Toast.makeText(this, "All Calender from your mobile deleted sucessfully",
                    Toast.LENGTH_LONG).show();
}

void deleteAllSms(){
    Log.i(TAG, "In deleteAllSms()");
    Uri uri_sms = Uri.parse("content://sms");;
    String str_column_name = "_id";
    String[] projection = {str_column_name};
    int columnIndex = 0;
    String str_id = "";
    Vector<String> vector_id = new Vector<String>();
    int delRow = 0;
    String where = "";
    try {
            Cursor cursor = cr.query(uri_sms, projection, null, null, null);
            if(cursor.moveToFirst()){
                    do{
                            columnIndex = cursor.getColumnIndex(str_column_name);
                            str_id = cursor.getString(columnIndex);
                            vector_id.add(str_id);
                    }while(cursor.moveToNext());
            }
            cursor.close();
            for(int i=0; i<vector_id.size(); i++){
                    str_id = vector_id.get(i);
                    where = str_column_name+"="+str_id;
                    delRow = cr.delete(uri_sms, where, null);
                    Log.i(TAG, "deleteAllSms(),delRow:"+delRow);
            }
            } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Log.e(TAG, "deleteAllSms(),Exception");
                    e.printStackTrace();
            }
            Log.i(TAG, "Out deleteAllSms()");
            Toast.makeText(this, "All Sms from your mobile deleted sucessfully",
                    Toast.LENGTH_LONG).show();
}
}


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate