1
我想使用我的設備中的數據庫設置鈴聲。我的應用程序能夠從設備獲取鈴聲並將其存儲在數據庫中。現在,我想要做的是無論我通過微調器選擇的鈴聲,它設置爲我的默認鈴聲。如何使用設備上的數據庫設置鈴聲?
這裏是我的代碼
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);} }
我能做到不使用烏里@ imran ?? –
@JasmeetKaurChauhan:setActualDefaultRingtoneUri(上下文環境,int類型,Uri ringtoneUri)將第二個參數作爲Uri使用,所以我認爲這不適用於Uri。 –
@JasmeetKaurChauhan:你目前如何在數據庫中存儲鈴聲? –