2012-03-25 69 views
1

目前我可以设置的铃声和通知:Android的通知增加了复制到Android通知声音微调

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/TEST/"; 
    String filename = MediaStore.MediaColumns.TITLE + ".mp3"; 

    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, "TEST:RingTone"); 
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
    values.put(MediaStore.Audio.Media.ARTIST, "cssounds "); 
    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); 

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

    RingtoneManager.setActualDefaultRingtoneUri(this, 
      RingtoneManager.TYPE_RINGTONE, newUri); 
    Toast.makeText(this, "Saved as Ringtone", Toast.LENGTH_SHORT).show(); 
    return true; 

的问题是它继续同TEST:Notification每次添加一个新的通知程序(或铃声)设置与只添加一个。我认为它这样做这一行:

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

但我不知道如何设置一个检查,看看如果文件名已经在Android系统的通知微调创建。

回答

1

我找到了答案here和工作的代码是:

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()); 
    getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null); 
    Uri newUri = getContentResolver().insert(uri, values);