As the company recently wants to develop an alarm clock, it needs to provide the function of selecting the built-in ringtone and custom ringtone in the system. First, we made a simple demo.
Package COM. example. mymusiclist; import Java. util. arraylist; import Java. util. list; import android.net. uri; import android. OS. bundle; import android. provider. mediastore; import android. app. activity; import android. content. contentresolver; import android. database. cursor; import android. util. log; import android. view. layoutinflater; import android. view. menu; import android. view. view; import android. view. viewgroup; Import android. widget. baseadapter; import android. widget. listview; import android. widget. textview; import android. widget. toast; public class mainactivity extends activity {list <string> list_ringtone = new arraylist <string> (); List <string> list_ringtonepath = new arraylist <string> (); list <string> list_music = new arraylist <string> (); List <string> list_musicpath = new arraylist <string> (); List <string> List_alarm = new arraylist <string> (); List <string> list_alarmpath = new arraylist <string> (); List <string> list_notification = new arraylist <string> (); list <string> list_icationicationpath = new arraylist <string> (); Private listview lv_listview; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); initdata (); initwi DGET ();} // instantiate the control private void initwidget () {lv_listview = (listview) This. findviewbyid (R. id. listview1); mybaseadapter MBA = new mybaseadapter (list_music); // mybaseadapter MBA = new mybaseadapter (list_ringtone); // mybaseadapter MBA = new mybaseadapter (list_notification ); // mybaseadapter MBA = new mybaseadapter (list_alarm); lv_listview.setadapter (MBA);} // initialize private void initdata () {// getallmus IC (); // scannermediafile ();}/*** scan Android system ringtones */private void scannermediafile () {// notification contentresolver Cr = This. getcontentresolver (); cursor = CR. query (mediastore. audio. media. internal_content_uri, new string [] {mediastore. audio. media. _ id, mediastore. audio. media. data, mediastore. audio. media. title}, "is_notification! =? ", New string [] {" 0 "}," _ id ASC "); If (cursor = NULL) {return;} while (cursor. movetonext () {list_icationication.add (cursor. getstring (1);} // alarm cursor = CR. query (mediastore. audio. media. internal_content_uri, new string [] {mediastore. audio. media. _ id, mediastore. audio. media. data, mediastore. audio. media. title}, "is_alarm! =? ", New string [] {" 0 "}," _ id ASC "); If (cursor = NULL) {return;} while (cursor. movetonext () {list_alarm.add (cursor. getstring (1);} // ringtone cursor = CR. query (mediastore. audio. media. internal_content_uri, new string [] {mediastore. audio. media. _ id, mediastore. audio. media. data, mediastore. audio. media. title}, "is_ringtone! =? ", New string [] {" 0 "}," _ id ASC "); If (cursor = NULL) {return;} while (cursor. movetonext () {list_ringtone.add (cursor. getstring (1);} // music cursor = CR. query (mediastore. audio. media. internal_content_uri, new string [] {mediastore. audio. media. _ id, mediastore. audio. media. data, mediastore. audio. media. title}, "is_music! =? ", New string [] {" 0 "}," _ id ASC "); If (cursor = NULL) {return;} while (cursor. movetonext () {list_music.add (cursor. getstring (1);} cursor. close ();}/*** get all the music in the Android phone */private void getallmusic () {contentresolver Cr = getcontentresolver (); // contentprovider can only send the request URI audio_uri = mediastore by contentresolver. audio. media. external_content_uri; // URI audio_uri = mediastore. audio. media. exter Nal_content_uri; // obtain the URI of the audio file, // video mediastore. video. media. external_content_uri // image mediastore. images. media. external_content_uri string [] Columns = new string [] {mediastore. audio. media. title, mediastore. audio. media. data}; // query field // name of the column to be read. These constants can be found in the official Google development documentation. The title is "data is the path cursor = CR. query (audio_uri, columns, null, null, mediastore. audio. media. default_sort_order); // query // cursor = CR. Query (audio_uri, columns, mediastore. Audio. Media. Duration + ">? ", New string [] {" 10000 "}, mediastore. audio. media. default_sort_order); // the query with where is the same as the query SQL, except for the first parameter. filter files smaller than 10 seconds based on the duration. If (cursor! = NULL) {While (cursor. movetonext () {// read the first column cyclically, that is, the file path. Column 0 is the title list_music.add (cursor. getstring (1); // list_musicpath.add (cursor. getstring (1);} cursor. close () ;}@ override public Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. main, menu); Return true;} private class mybaseadapter extends baseadapter {private list <string> data; Public mybaseadapter (list <string> data) {This. data = data ;}@ override public int getcount () {return this. data. size () ;}@ override public object getitem (INT arg0) {return NULL ;}@ override public long getitemid (INT arg0) {return 0 ;} @ override public view getview (INT position, view converview, viewgroup arg2) {viewhodler; If (converview = NULL) {viewhodler = new viewhodler (); converview = layoutinflater. from (getapplicationcontext ()). inflate (R. layout. test_tem_textview, null); viewhodler. ringname = (textview) converview. findviewbyid (R. id. textview1); converview. settag (viewhodler);} else {viewhodler = (viewhodler) converview. gettag ();} viewhodler. ringname. settext (this. data. get (position); Return converview;} private class viewhodler {textview ringname ;}}
: