2012-03-25 70 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);