MediaStore Content Provider

Following code demonstrates retrieving audio files stored in sdcard and displaying list in listview. When user double clicks any of the song that music file will be played.


public class Test1Activity extends Activity 
{
/** Called when the activity is first created. */
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	init_phone_music_grid();
}
private void init_phone_music_grid() {
	System.gc();
	String[] proj = { 
                MediaStore.Audio.Media._ID,
		MediaStore.Audio.Media.DATA,
		MediaStore.Audio.Media.DISPLAY_NAME,
		MediaStore.Video.Media.SIZE };
	musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
			proj, null, null, null);
	count = musiccursor.getCount();
	musiclist = (ListView) findViewById(R.id.PhoneMusicList);
	musiclist.setAdapter(new MusicAdapter(getApplicationContext()));

	musiclist.setOnItemClickListener(musicgridlistener);
	mMediaPlayer = new MediaPlayer();
}

private OnItemClickListener musicgridlistener = new OnItemClickListener() {
	@Override
	public void onItemClick(AdapterView parent, View v, int position,
				long id) {
	// TODO Auto-generated method stub
	System.gc();
	music_column_index = musiccursor
		.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
	musiccursor.moveToPosition(position);
	String filename = musiccursor.getString(music_column_index);
	try {
		if (mMediaPlayer.isPlaying()) {
			mMediaPlayer.reset();
		}
		mMediaPlayer.setDataSource(filename);
		mMediaPlayer.prepare();
		mMediaPlayer.start();
	} catch (Exception e) {}

	}
};

public class MusicAdapter extends BaseAdapter {
	private Context mContext;

	public MusicAdapter(Context c) {
		mContext = c;
	}
	public int getCount() {
		return count;
	}
	public Object getItem(int position) {
		return position;
	}
	public long getItemId(int position) {
		return position;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		System.gc();
		TextView tv = new TextView(mContext.getApplicationContext());
		String id = null;
		if (convertView == null) {
		music_column_index = musiccursor
		.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
			musiccursor.moveToPosition(position);
		id = musiccursor.getString(music_column_index);
		music_column_index = musiccursor
			.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
		musiccursor.moveToPosition(position);
		id += " Size(KB):" + musiccursor.getString(music_column_index);
		tv.setText(id);
		} else
			tv = (TextView) convertView;
			return tv;
		}
	}
}

Contacts Content Provider

Following code demonstrates retrieving information such as no. of contacts using Contacts Content Provider.

Write the following code in onCreate() method.

Cursor oneContact = managedQuery( People.CONTENT_URI, null, null, null, null);
Log.d("TEMP2", "Count: " + oneContact.getCount());
Log.d("TEMP2", "column Count: " + oneContact.getColumnCount());

CallLog Content Provider

Following code demonstrates retrieving information such as caller label, duration of the call using CallLog Content Provider.

Write the following code in onCreate() method.

String[] requestedColumns =
{
	CallLog.Calls.CACHED_NUMBER_LABEL,
	CallLog.Calls.DURATION
};
Cursor calls = managedQuery(CallLog.Calls.CONTENT_URI,
		requestedColumns, null, null, null);

Log.i(DEBUG_TAG, "Call count: " + calls.getCount());

int durIdx = calls.getColumnIndex(CallLog.Calls.DURATION);
int totalDuration = 0;
calls.moveToFirst();
while (!calls.isAfterLast())
{
	Log.i(DEBUG_TAG, "Duration: " + calls.getInt(durIdx));
			totalDuration += calls.getInt(durIdx);
	calls.moveToNext();
}
Toast.makeText(getApplicationContext(),
		"inside while " + totalDuration, 20).show();
Log.i(DEBUG_TAG, "Call Duration: " + totalDuration);

Browser Content Provider

Following code demonstrates retrieving visited website title, no. of times that website visited and whether it is bookmarked or not.

Write the following code in onCreate() method.

String[] requestedColumns = 
{
	Browser.BookmarkColumns.TITLE,
	Browser.BookmarkColumns.VISITS,
	Browser.BookmarkColumns.BOOKMARK
};
Cursor faves = managedQuery(Browser.BOOKMARKS_URI, requestedColumns,
null, null, null);
Log.d(DEBUG_TAG, "Bookmarks count: " + faves.getCount());
int titleIdx = faves.getColumnIndex(Browser.BookmarkColumns.TITLE);
int visitsIdx = faves.getColumnIndex(Browser.BookmarkColumns.VISITS);
int bmIdx = faves.getColumnIndex(Browser.BookmarkColumns.BOOKMARK);
faves.moveToFirst();
int count = 0;
while (!faves.isAfterLast())
{
	Log.d(DEBUG_TAG, "Inside while --- : " + count);
	Log.d("SimpleBookmarks",faves.getString(titleIdx) + " visited "
		+ faves.getInt(visitsIdx) + " times : "
		+ (faves.getInt(bmIdx) != 0 ? "true" : "false"));
	faves.moveToNext();
	count++;
}