我想使用我设备中的数据库设置铃声。我的应用程序能够从设备中获取铃声并将其存储在数据库中。现在我想要做的就是我通过微调器选择的任何铃声,它设置为我的默认铃声。
这是我的代码
public class SMSReceiver extends BroadcastReceiver{
DBAdapter db ;
int vol = 7;
String code="";
@Override
public void onReceive(Context context, Intent intent) {
contextx=context;
Bundle bundle = intent.getExtras();
// TODO Auto-generated method stub
db = new DBAdapter(context);
db.open();
Cursor c = db.getAllTitles();
int row_size = c.getCount();
System.out.println("row size++++++++"+row_size);
if(row_size>0)
{
c.moveToFirst();
code = c.getString(1);
String status = c.getString(2);
String ring = c.getString(3);
vol = c.getInt(4);
String mode = c.getString(5);
System.out.println("code++++++++"+code);
System.out.println("status++++++++"+status);
System.out.println("ring++++++++"+ring);
System.out.println("vol++++++++"+vol);
System.out.println("mode++++++++"+mode);
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_RINGTONES);
File Ringtonefile = new File(path,ring);
setMyRingtone(Ringtonefile) ;
}
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
if(smsMessage[0].getMessageBody().equals(""+code))
{
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, vol, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
}
}
public void setMyRingtone(File file)
{
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, file.getName());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
Uri newUri = contextx.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(contextx, RingtoneManager.TYPE_RINGTONE, newUri);} }