Code example for ContentResolver

Methods: deleteinsertquery

0
	 */ 
	public static long getPlaylist(ContentResolver resolver, String name)
	{ 
		long id = -1;
 
		Cursor cursor = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
			new String[] { MediaStore.Audio.Playlists._ID },
			MediaStore.Audio.Playlists.NAME + "=?",
			new String[] { name }, null);
 
		if (cursor != null) {
			if (cursor.moveToNext())
				id = cursor.getLong(0);
			cursor.close();
		} 
 
		return id;
	} 
 
	/** 
	 * Create a new playlist with the given name. If a playlist with the given 
	 * name already exists, it will be overwritten. 
	 * 
	 * @param resolver A ContentResolver to use. 
	 * @param name The name of the playlist. 
	 * @return The id of the new playlist. 
	 */ 
	public static long createPlaylist(ContentResolver resolver, String name)
	{ 
		long id = getPlaylist(resolver, name);
 
		if (id == -1) {
			// We need to create a new playlist. 
			ContentValues values = new ContentValues(1);
			values.put(MediaStore.Audio.Playlists.NAME, name);
			Uri uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values);
			id = Long.parseLong(uri.getLastPathSegment());
		} else { 
			// We are overwriting an existing playlist. Clear existing songs. 
			Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", id);
			resolver.delete(uri, null, null);
		} 
 
		return id;
	}