Reputation: 175
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 ?
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
Upvotes: 0
Views: 2426
Reputation: 11
To read the contacts use the foll code:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/contactview"
/>
</LinearLayout>
In manifest file add the followinf entry:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
the java code to display contact names from the contact book is given above:
Upvotes: 0
Reputation: 11
ViewContact.java:
Note: getContacts()
is an user defined function
package com.android.ViewContact;
import android.app.Activity;
import android.os.Bundle;
import android.database.*;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.TextView;
public class ViewContactAct extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView contactview=(TextView)findViewById(R.id.contactview);
Cursor mycursor=getContacts();
while(mycursor.moveToNext())
{
String displayname=mycursor.getString(mycursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
contactview.append("Name: ");
contactview.append(displayname);
contactview.append("\n");
}
}
private Cursor getContacts()
{
Uri myuri=ContactsContract.Contacts.CONTENT_URI;
String[] projection=new String[]
{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection= ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(myuri, projection, selection, selectionArgs,
sortOrder);
}
}
Upvotes: 1
Reputation: 4701
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.
Upvotes: 0