tags:

views:

749

answers:

2

I am new for android development. I am trying read and write contacts to android addressbook.

I tried following line of code for write name into android

public class SecondApp extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

ContentValues values = new ContentValues();
values.put(Contacts.People.NAME, "Rishabh");
ContentResolver cr = getContentResolver();
Uri uri = cr.insert(People.CONTENT_URI, values);

but i am getting "The application has stopped unexpectedly. Please try again" message.

what is wrong in it ? How can i access contacts of android ?

A: 

You are missing system permissions to write a contact. Add this line to your manifest file:

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

If your code has to read contacts, your applications will need "android.permission.READ_CONTACTS".

Full set of permissions flags can be found on Android reference pages.

MannyNS
A: 

Hi MannyNS thanks for reply I have given write permission as you defined above.

but i am still getting the "The application has stopped unexpectedly. Please try again", even when i start android its giving message "Process android.process media is not responding".

Can you suggest me whole procedure to read and write contacts to android ? Do you have any sample code for it ?

Thanks Rishabh

Rishabh
What process is not responding? android.process.acore? How long do you wait?Your example, though not perfect, should work fine. Some links with more information: http://developer.android.com/reference/android/content/ContentProvider.htmlhttp://www.eli.sdsu.edu/courses/fall08/cs683/notes/Android5.pdfPlease note that this is not a discussion group, so if you have any comment, use the comment feature.
MannyNS
Again, use LogCat to find the root cause of crash.
bhatt4982
How about you look at the stack trace first to find out what is causing this...?
Matthias