Android通知会向Android通知声音微调器添加重复内容

时间:2012-03-25 01:44:16

标签: 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 个答案:

答案 0 :(得分: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);