2011-03-25 477 views
2

我正在开发一款Android应用程序,该应用程序允许用户长按一个按钮以将铃声保存为铃声。我正在使用下面的代码来这样做。该代码目前用于将文件保存在要使用的铃声列表中,但它不会自动将声音设置为默认铃声。我搜索了所有的东西,没有多少运气找到一个明确的指导,保存声音作为默认/活动铃声。Android:铃声正在保存,但没有设置为活动/默认铃声

截至目前,用户可以长按按钮,然后进入菜单>声音>手机铃声菜单,并从列表中选择,但这似乎有点不方便,当我知道它可能有它只需将其设置为主动铃声即可。

任何有关我缺少的知识?非常感激!

public boolean saveas(int ressound){ 
     byte[] buffer=null; 
     InputStream fIn = getBaseContext().getResources().openRawResource(ressound); 
     int size=0; 

     try { 
     size = fIn.available(); 
     buffer = new byte[size]; 
     fIn.read(buffer); 
     fIn.close(); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     return false; 
     } 

     String path="/sdcard/media/audio/ringtones/"; 
     String filename="ADTone"+".ogg"; 

     boolean exists = (new File(path)).exists(); 
     if (!exists){new File(path).mkdirs();} 

     FileOutputStream save; 
     try { 
     save = new FileOutputStream(path+filename); 
     save.write(buffer); 
     save.flush(); 
     save.close(); 
     } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     return false; 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     return false; 
     }  

     sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename))); 

     File k = new File(path, filename); 

     ContentValues values = new ContentValues(); 
     values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); 
     values.put(MediaStore.MediaColumns.TITLE, "AD Ringtone"); 
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg"); 
     values.put(MediaStore.Audio.Media.ARTIST, "adtone "); 
     values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
     values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); 
     values.put(MediaStore.Audio.Media.IS_ALARM, true); 
     values.put(MediaStore.Audio.Media.IS_MUSIC, false); 

     //Insert it into the database 
     this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values); 


     return true; 
    } 

回答

2

不知道你是否想出了这一个,但我最近才做。用您的插入数据库行代替:

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()); 

      getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null); 

      Uri newUri = getContentResolver().insert(uri, values); 

      RingtoneManager.setActualDefaultRingtoneUri(
        YOURACTIVITYNAME.this, 
       RingtoneManager.TYPE_RINGTONE, 
       newUri 
      );