예제 #1
0
  public static void setRingtone(Media song, Activity activity) {
    File newringtone = Util.URItoFile(song.getLocation());
    if (!newringtone.exists()) {
      Toast.makeText(
              activity.getApplicationContext(),
              activity.getString(R.string.ringtone_error),
              Toast.LENGTH_SHORT)
          .show();
      return;
    }

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, newringtone.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, song.getTitle());
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    values.put(MediaStore.Audio.Media.ARTIST, song.getArtist());
    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(newringtone.getAbsolutePath());
    Uri newUri;
    try {
      activity
          .getContentResolver()
          .delete(
              uri,
              MediaStore.MediaColumns.DATA + "=\"" + newringtone.getAbsolutePath() + "\"",
              null);
      newUri = activity.getContentResolver().insert(uri, values);
      RingtoneManager.setActualDefaultRingtoneUri(
          activity.getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
    } catch (Exception e) {
      Toast.makeText(
              activity.getApplicationContext(),
              activity.getString(R.string.ringtone_error),
              Toast.LENGTH_SHORT)
          .show();
      return;
    }

    Toast.makeText(
            activity.getApplicationContext(),
            activity.getString(R.string.ringtone_set, song.getTitle()),
            Toast.LENGTH_SHORT)
        .show();
  }
  // 设置--闹铃音的具体实现方法
  public void setMyAlarm() {
    File sdfile = new File(musicname);
    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    values.put(MediaStore.Audio.Media.IS_ALARM, true);
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);

    Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());
    Uri newUri = this.getContentResolver().insert(uri, values);
    RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM, newUri);
    Toast.makeText(getApplicationContext(), "设置闹钟铃声成功!", Toast.LENGTH_SHORT).show();
    System.out.println("setMyNOTIFICATION------闹铃音");
  }