android 选择系统自带铃声和选择自定义铃声
由于公司最近要开发一个闹钟,需要提供选择系统自带铃声和自定义铃声的功能,先做了一个最简单的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_notificationPath = new ArrayList<String>(); private ListView lv_listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initWidget(); } //实例化控件 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); } //初始化数据 private void initData(){ // getAllMusic(); // scannerMediaFile(); } /** * 扫描android的系统铃声 */ private void scannerMediaFile() { //notification ContentResolver cr = this.getContentResolver(); Cursor 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_notification.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(); } /** * 获取android手机中全部的音乐 */ private void getAllMusic(){ ContentResolver cr = getContentResolver(); //ContentProvider只能由ContentResolver发送请求 Uri AUDIO_URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; // Uri AUDIO_URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; //获取音频文件的URI, //视频 MediaStore.Video.Media.EXTERNAL_CONTENT_URI //图片MediaStore.Images.Media.EXTERNAL_CONTENT_URI String[] columns = new String[] {MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA};//查询的字段 //要读的列名,这些常量可以查GOOGLE官方开发文档,TITLE是标题 DATA是路径 Cursor cursor = cr.query(AUDIO_URI, columns, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);//查询 // Cursor cursor = cr.query(AUDIO_URI, columns, MediaStore.Audio.Media.DURATION+">?", new String[]{"10000"}, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);//带where的查询 //跟查询SQL一样了,除了第一个参数不同外.后面根据时长过滤小于10秒的文件 if(cursor != null){ while(cursor.moveToNext()){ //循环读取第一列,即文件路径,0列是标题 list_music.add(cursor.getString(1)); // list_musicPath.add(cursor.getString(1)); } cursor.close(); } } @Override public boolean onCreateOptionsMenu(Menu 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 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; } }
效果图: