A song specified on the android playback SD card (Basic Edition)

Source: Internet
Author: User
Tags sqlite query

Before talking about the topic, let's add a little knowledge about SQLite query statements (the android database uses SQLite ).

Like Operator

A useful relational operator is like. Like is similar to equal (=), but it uses a template to perform string matching. For example, to query all foods whose names start with the character "J", use the following statement:

SQLite> select ID, name from foods where name like 'J % ';

ID name

156 juice box

236 Juicy Fruit gum

243 jello with bananas

244 jujyfruit

245 Junior Mints

370 jambalaya

The percent sign (%) in the template can match any 0 to multiple characters. The underscore (_) can match any single character.

-- Reference from SQLite authoritative guide

Statement description:This statement returns the data of the ID and name columns in the foods table. The returned data must meet the following requirements: the data is a string starting with J on the name attribute.

Corresponding to this, let's look at the query statement query () in Android ().

Contentresolver. Query (URI Uri, string [] projection, string [] selection, string [] selectionargs, string sortorder );

Uri is the table name.

Projection is the array of column names to return.

Selection is a constraint, which is equivalent to a statement after the where keyword, such as name like 'J % '(Note: strings in SQLite are enclosed by single quotation marks)

Selectionargs the string in this array is used to replace? (For example, the selection statement name =? And phone =? Contains two? , Two string parameters need to be introduced in selectionargs, such as new string [] {myname, Myphone })

Sorting method of data returned by sortorder

------------------

Now, go to the topic:

BelowProgramTo add a music background whose name ends with "sorry" for the androidtest program.

This music starts playing in the oncreat () and onrestart () statuses of the activity. It stops playing when the activity enters the onstop () status.

Package kenan.com. test; import android. app. activity; import android. content. contentresolver; import android. database. cursor; import android. media. mediaplayer; import android.net. uri; import android. OS. bundle; import android. provider. mediastore;/*** @ classname: androidtestactivity * @ Description: Set a background music for this activity. * @ author jogging Android * @ date 2011-11-22 04:12:00 */public class Andro Idtestactivity extends activity {private mediaplayer; private final URI musictableforsd = mediastore. audio. media. external_content_uri; private final string musictitle = mediastore. audio. audiocolumns. title; private final string musicid = mediastore. audio. media. _ id; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main ); Playbackgroundmusic () ;}@ overrideprotected void onrestart () {super. onrestart (); playbackgroundmusic () ;}@ overrideprotected void onstop () {super. onstop (); stopmusic ();} private void playbackgroundmusic () {// get a contentresovler, // so that we can read the music information form SD card. contentresolver = getcontentresolver (); string musicname = "% Sorry sorry"; cursor = Conte Ntresolver. Query (musictableforsd, new string [] {musicid, musictitle}, musictitle + "like? ", New string [] {musicname}, null); // If cursor has a recode, we support that we find the music. if (cursor. movetonext () {// get the music ID. int position = cursor. getint (cursor. getcolumnindex (musicid); Uri uri = Uri. withappendedpath (musictableforsd, "/" + position); mediaplayer = mediaplayer. create (this, Uri); If (null! = Mediaplayer) {mediaplayer. Start () ;}// finish our query work. cursor. Close () ;}private void stopmusic () {If (null! = Mediaplayer) {mediaplayer. Stop (); mediaplayer. Release ();}}}

References:

Android gets sdcard music files

Usage of selectionargs in query () in the database query method in Android

Beginner Tutorial: Android built-in SQLite Operation Method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.