public class AlarmInfo {
private long id;
private String title;
private String data;
public long getId ( ) {
return id;
}
public void setId ( long id) {
this . id = id;
}
public String getTitle ( ) {
return title;
}
public void setTitle ( String title) {
this . title = title;
}
public String getData ( ) {
return data;
}
public void setData ( String data) {
this . data = data;
}
}
通过ContentResolver查询数据存储到model(结合更改系统闹钟铃声为例)
private List< AlarmInfo> alarmInfos = new ArrayList < > ( ) ;
private static final Uri URI = MediaStore. Audio. Media. INTERNAL_CONTENT_URI;
private static final String[ ] PROJECTION = {
MediaStore. Audio. Media. _ID,
MediaStore. Audio. Media. DATA,
MediaStore. Audio. Media. TITLE
} ;
private static final String SELECTION = "is_alarm != ?" ;
private static final String[ ] SELECTION_ARGS = { "0" } ;
private static final String SORT_ORDER = "_id asc" ;
findViewById ( R. id. alarmLayout) . setOnClickListener ( new OnClickListener ( ) {
@Override
public void onClick ( View view) {
scanMediaFile ( ) ;
showSetAlarmDialog ( ) ;
}
} ) ;
private void scanMediaFile ( ) {
Cursor cursor = getContentResolver ( ) . query ( URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER) ;
if ( cursor == null) return ;
while ( cursor. moveToNext ( ) ) {
AlarmInfo info = new AlarmInfo ( ) ;
long id = cursor. getLong ( cursor. getColumnIndex ( MediaStore. Audio. Media. _ID) ) ;
String data = cursor. getString ( cursor. getColumnIndex ( MediaStore. Audio. Media. DATA) ) ;
String title = cursor. getString ( cursor. getColumnIndex ( MediaStore. Audio. Media. TITLE) ) ;
info. setId ( id) ;
if ( ! TextUtils. isEmpty ( data) ) info. setData ( data) ;
if ( ! TextUtils. isEmpty ( title) ) info. setTitle ( title) ;
alarmInfos. add ( info) ;
}
cursor. close ( ) ;
}
private String selectAlarm;
private MediaPlayer mMediaPlayer;
private void showSetAlarmDialog ( ) {
if ( alarmInfos. size ( ) > 0 ) {
final String[ ] alarms = new String [ alarmInfos. size ( ) ] ;
for ( int i = 0 ; i < alarmInfos. size ( ) ; i++ ) {
alarms[ i] = alarmInfos. get ( i) . getTitle ( ) ;
}
int checkItem = - 1 ;
new AlertDialog. Builder ( this , R. style. CustomAlertDialogStyle)
. setTitle ( "请选择要设置的铃声" )
. setSingleChoiceItems ( alarms, checkItem, new DialogInterface. OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog, int which) {
stopBroadcastAlarm ( ) ;
selectAlarm = alarmInfos. get ( which) . getData ( ) ;
startBroadcastAlarm ( Uri. parse ( selectAlarm) ) ;
}
} )
. setPositiveButton ( "确定" , new DialogInterface. OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog, int which) {
dialog. dismiss ( ) ;
stopBroadcastAlarm ( ) ;
RingtoneManager. setActualDefaultRingtoneUri ( mContext, RingtoneManager. TYPE_ALARM,
Uri. parse ( selectAlarm) ) ;
showToast ( "设置成功!" ) ;
}
} )
. setNegativeButton ( "取消" , new DialogInterface. OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog, int which) {
dialog. dismiss ( ) ;
stopBroadcastAlarm ( ) ;
}
} ) . show ( ) ;
}
}
private void startBroadcastAlarm ( Uri uri) {
try {
mMediaPlayer = new MediaPlayer ( ) ;
mMediaPlayer. setDataSource ( this , uri) ;
mMediaPlayer. prepare ( ) ;
mMediaPlayer. start ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
private void stopBroadcastAlarm ( ) {
if ( mMediaPlayer != null && mMediaPlayer. isPlaying ( ) ) {
mMediaPlayer. stop ( ) ;
}
}
< uses- permission android: name= "android.permission.VIBRATE" / >
< uses- permission android: name= "android.permission.WRITE_SETTINGS" / >