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);} }
4

1 回答 1

0

尝试使用RingtoneManager设置默认铃声:

public class SMSReceiver extends BroadcastReceiver{
Context contextx;
    DBAdapter db ;
    int vol = 7;
    String code="";
        @Override
    public void onReceive(Context context, Intent intent) { 
      this.contextx=context;
      //your code....


    File Ringtonefile = new File(path, "Ringtone.mp3");
    setMyRingtone(Ringtonefile) ;
    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.SIZE, file.length());  
           values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");  
    //       values.put(MediaStore.Audio.Media.ARTIST, "Madonna");  
    //       values.put(MediaStore.Audio.Media.DURATION, 230);  
           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);  
         } 

manifast.xml 权限:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
于 2012-06-26T05:52:54.253 回答