Search This Blog

Thursday 5 July 2012

Get Social Updates of your contact list using Ice cream sandwich


Social Stream API in Contacts Provider


Android 4.0.3 API Level (15) is released for the developers with a new support for social streams in contact list. Now you can add the support to update and manage the social activities from facebook,twitter and other social networks and update the contacts data with simple APIs. 

This is really a good stuff added in Android to create good applications around the social networks. Here in this post we are highlighting the APIs you can use to create the new exciting apps with your social networks.

Now you could get the status updates, check-ins , activities and photo updates from social stream and associate this data with your contact list.


The database table for contacts now have two more Uris ContactsContract.StreamItems and ContactsContract.StreamItemPhotos.


In order to read and write the social streams you need to add two more permissions to your application manifest file.


<uses-permission android:name="android.permission.READ_SOCIAL_STREAM"> 
<uses-permission android:name="android.permission.WRITE_SOCIAL_STREAM">


Social stream updates are always associated with the raw contacts and can be insert,update,delete or retrieve.

You can use the content Uris to add the social stream with raw contact
 ContentValues values = new ContentValues();
 values.put(StreamItems.RAW_CONTACT_ID, rawContactId);
 values.put(StreamItems.TEXT, "Lunch at 3.00 PM");
 values.put(StreamItems.TIMESTAMP, timestamp);
 values.put(StreamItems.COMMENTS, "Family and Friends");
 Uri.Builder builder = StreamItems.CONTENT_URI.buildUpon();
 builder.appendQueryParameter(RawContacts.ACCOUNT_NAME, accountName);
 builder.appendQueryParameter(RawContacts.ACCOUNT_TYPE, accountType);
 Uri streamItemUri = getContentResolver().insert(builder.build(), values);
 long streamItemId = ContentUris.parseId(streamItemUri);

Now you can associate the photos with social updates
 values.clear();
 values.put(StreamItemPhotos.STREAM_ITEM_ID, streamItemId);
 values.put(StreamItemPhotos.SORT_INDEX, 1);
 values.put(StreamItemPhotos.PHOTO, photoData);
 getContentResolver().insert(StreamItems.CONTENT_PHOTO_URI, values);

You can retrieve the social updates of the raw contact id as follows

 Cursor c = getContentResolver().query(Uri.withAppendedPath(
          ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
          RawContacts.StreamItems.CONTENT_DIRECTORY)),
          null, null, null, null);
 

Now get ready to build a quick App using the above APIs.

Please provide your valuable comments to improve this post.

Creative Android Apps

1 comment:

  1. This is the consensus for every people that we should read daily new things if we want learn something new. Same as this is the post to provide new way about this topic along with here is another topic, which belongs to Scan Your system using free anti malware tool from your computer device and I personally touched after this post so you must read it once.

    ReplyDelete