Cdacians

Cdacians
Cdacians

Tuesday, 7 August 2012

Add contact using Contacts content provider


Add contact using Contacts content provider

Add contact using Contacts content provider

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.AddContact;
 
import android.app.Activity;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class AddContactActivity extends Activity {
  
 EditText inputName, inputNumber;
 Button buttonAdd;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        inputName = (EditText)findViewById(R.id.name);
        inputNumber = (EditText)findViewById(R.id.number);
         
        inputName.setText("Coding");
        inputNumber.setText("12345678");
         
     buttonAdd = (Button)findViewById(R.id.add);
      
     buttonAdd.setOnClickListener(new Button.OnClickListener(){
 
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
     
    String contactName = inputName.getText().toString();
    String contactNumber = inputNumber.getText().toString();
     
    ContentValues contentValues = new ContentValues();
    contentValues.put(People.NAME, contactName);
    contentValues.put(People.STARRED, 1);
    Uri newContactUri = getContentResolver().insert(People.CONTENT_URI, contentValues);
     
    Uri phoneUri = Uri.withAppendedPath(newContactUri, People.Phones.CONTENT_DIRECTORY);
    contentValues.clear();
    contentValues.put(People.Phones.TYPE, People.TYPE_MOBILE);
    contentValues.put(People.NUMBER, contactNumber);
     
    getContentResolver().insert(phoneUri, contentValues);
     
    Toast.makeText(AddContactActivity.this,
      contactName + " : " + contactNumber,
      Toast.LENGTH_LONG).show();
   }});
    }
}



akm
www.cdacians.com

No comments:

Post a Comment