Java Code Examples for android.media.RingtoneManager#setActualDefaultRingtoneUri()

The following examples show how to use android.media.RingtoneManager#setActualDefaultRingtoneUri() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalMusicFragment.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 设置铃声
 */
private void setRingtone(AudioBean localMusic) {
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(localMusic.getPath());
    // 查询音乐文件在媒体库是否存在
    Cursor cursor = getContext().getContentResolver().query(uri, null,
            MediaStore.MediaColumns.DATA + "=?", new String[]{localMusic.getPath()}, null);
    if (cursor == null) {
        return;
    }
    if (cursor.moveToFirst() && cursor.getCount() > 0) {
        String _id = cursor.getString(0);
        ContentValues values = new ContentValues();
        values.put(MediaStore.Audio.Media.IS_MUSIC, true);
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        values.put(MediaStore.Audio.Media.IS_ALARM, false);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        values.put(MediaStore.Audio.Media.IS_PODCAST, false);

        getContext().getContentResolver().update(uri, values, MediaStore.MediaColumns.DATA + "=?",
                new String[]{localMusic.getPath()});
        Uri newUri = ContentUris.withAppendedId(uri, Long.valueOf(_id));
        RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);
        ToastUtils.showShort("设置铃声成功");
    }
    cursor.close();
}
 
Example 2
Source File: SettingsHelper.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the ringtone of type specified by the name.
 *
 * @param name should be Settings.System.RINGTONE or Settings.System.NOTIFICATION_SOUND.
 * @param value can be a canonicalized uri or "_silent" to indicate a silent (null) ringtone.
 */
private void setRingtone(String name, String value) {
    // If it's null, don't change the default
    if (value == null) return;
    Uri ringtoneUri = null;
    if (SILENT_RINGTONE.equals(value)) {
        ringtoneUri = null;
    } else {
        Uri canonicalUri = Uri.parse(value);
        ringtoneUri = mContext.getContentResolver().uncanonicalize(canonicalUri);
        if (ringtoneUri == null) {
            // Unrecognized or invalid Uri, don't restore
            return;
        }
    }
    final int ringtoneType = Settings.System.RINGTONE.equals(name)
            ? RingtoneManager.TYPE_RINGTONE : RingtoneManager.TYPE_NOTIFICATION;
    RingtoneManager.setActualDefaultRingtoneUri(mContext, ringtoneType, ringtoneUri);
}
 
Example 3
Source File: SettingsHelper.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the ringtone of type specified by the name.
 *
 * @param name should be Settings.System.RINGTONE or Settings.System.NOTIFICATION_SOUND.
 * @param value can be a canonicalized uri or "_silent" to indicate a silent (null) ringtone.
 */
private void setRingtone(String name, String value) {
    // If it's null, don't change the default
    if (value == null) return;
    Uri ringtoneUri = null;
    if (SILENT_RINGTONE.equals(value)) {
        ringtoneUri = null;
    } else {
        Uri canonicalUri = Uri.parse(value);
        ringtoneUri = mContext.getContentResolver().uncanonicalize(canonicalUri);
        if (ringtoneUri == null) {
            // Unrecognized or invalid Uri, don't restore
            return;
        }
    }
    final int ringtoneType = Settings.System.RINGTONE.equals(name)
            ? RingtoneManager.TYPE_RINGTONE : RingtoneManager.TYPE_NOTIFICATION;
    RingtoneManager.setActualDefaultRingtoneUri(mContext, ringtoneType, ringtoneUri);
}
 
Example 4
Source File: MyMusicUtil.java    From MeetMusic with Apache License 2.0 6 votes vote down vote up
public static void setMyRingtone(Context context)
{
    DBManager dbManager = DBManager.getInstance(context);
    int musicId = MyMusicUtil.getIntShared(Constant.KEY_ID);
    String path = dbManager.getMusicPath(musicId);
    File sdfile = new File(path);
    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, 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(sdfile.getAbsolutePath());
    Uri newUri = context.getContentResolver().insert(uri, values);
    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
    Toast.makeText( context,"设置来电铃声成功!", Toast.LENGTH_SHORT ).show();
}
 
Example 5
Source File: MyMusicUtil.java    From AndroidDemo with MIT License 6 votes vote down vote up
public static void setMyRingtone(Context context)
{
    DBManager dbManager = DBManager.getInstance(context);
    int musicId = MyMusicUtil.getIntShared(Constant.KEY_ID);
    String path = dbManager.getMusicPath(musicId);
    File sdfile = new File(path);
    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, 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(sdfile.getAbsolutePath());
    Uri newUri = context.getContentResolver().insert(uri, values);
    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
    Toast.makeText( context,"设置来电铃声成功!", Toast.LENGTH_SHORT ).show();
}
 
Example 6
Source File: MainActivity.java    From music_player with Open Software License 3.0 6 votes vote down vote up
private void setAsRingtone(final Activity context, int position) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.System.canWrite(context)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + context.getPackageName()));
            context.startActivity(intent);
        } else {
            File music = new File(MyApplication.getMusicListNow().get(position).getMusicData()); // path is a file to /sdcard/media/ringtone
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, music.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, MyApplication.getMusicListNow().get(position).getMusicTitle());
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.ARTIST, MyApplication.getMusicListNow().get(position).getMusicArtist());
            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 uri = MediaStore.Audio.Media.getContentUriForPath(music.getAbsolutePath());
            context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + music.getAbsolutePath() + "\"", null);
            Uri newUri = context.getContentResolver().insert(uri, values);
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
            Toast.makeText(context, "已成功设置为来电铃声", Toast.LENGTH_SHORT).show();
            //Snackbar
//            Snackbar.make(mLayout, "已成功设置为来电铃声", Snackbar.LENGTH_LONG).show();
        }
    }
 
Example 7
Source File: searchAdapter.java    From music_player with Open Software License 3.0 5 votes vote down vote up
private void setAsRingtone(final Activity context, int position) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.System.canWrite(context)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + context.getPackageName()));
            context.startActivity(intent);
        } else {
//            List<musicInfo> list = MyApplication.getMusicInfoArrayList();
            File music = new File(musicInfoArrayList.get(position).getMusicData()); // path is a file to /sdcard/media/ringtone
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, music.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, musicInfoArrayList.get(position).getMusicTitle());
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.ARTIST, musicInfoArrayList.get(position).getMusicArtist());
            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 uri = MediaStore.Audio.Media.getContentUriForPath(music.getAbsolutePath());
            context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + music.getAbsolutePath() + "\"", null);
            Uri newUri = context.getContentResolver().insert(uri, values);
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
            //Snackbar
            Snackbar.make(rootview, "已成功设置为来电铃声", Snackbar.LENGTH_LONG).setAction("好的", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            }).show();
        }
    }
 
Example 8
Source File: FavouriteListAdapter.java    From music_player with Open Software License 3.0 5 votes vote down vote up
private void setAsRingtone(final Activity context, int position) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.System.canWrite(context)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + context.getPackageName()));
            context.startActivity(intent);
        } else {
            List<musicInfo> list = MyApplication.getTimessublist();
            File music = new File(list.get(position).getMusicData()); // path is a file to /sdcard/media/ringtone
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, music.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, list.get(position).getMusicTitle());
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.ARTIST, list.get(position).getMusicArtist());
            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 uri = MediaStore.Audio.Media.getContentUriForPath(music.getAbsolutePath());
            context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + music.getAbsolutePath() + "\"", null);
            Uri newUri = context.getContentResolver().insert(uri, values);
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
            Toast.makeText(context, "已成功设置为来电铃声", Toast.LENGTH_SHORT).show();
            //Snackbar
//            Snackbar.make(rootview, "已成功设置为来电铃声", Snackbar.LENGTH_LONG).setAction("好的", new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                }
//            }).show();
        }
    }
 
Example 9
Source File: MediaUtils.java    From IdealMedia with Apache License 2.0 5 votes vote down vote up
public static void setRingtone(Context context,Track track)
{

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, track.getPath());
    values.put(MediaStore.MediaColumns.TITLE, track.getTitle());
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    values.put(MediaStore.Audio.Media.ARTIST, track.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, true);

    Uri uri = MediaStore.Audio.Media.getContentUriForPath(track.getPath());

    if (context.getContentResolver() == null)
    {
        Toast.makeText(context, context.getString(R.string.set_as_ringtone_error), Toast.LENGTH_SHORT).show();
        return;
    }

    context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + track.getPath() + "\"", null);
    Uri newUri = context.getContentResolver().insert(uri, values);

    if (newUri == null) {
        Toast.makeText(context, context.getString(R.string.set_as_ringtone_error), Toast.LENGTH_SHORT).show();
    } else {
        RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
        Toast.makeText(context,context.getString(R.string.set_as_ringtone),Toast.LENGTH_SHORT).show();
    }
}
 
Example 10
Source File: FastScrollListAdapter.java    From music_player with Open Software License 3.0 5 votes vote down vote up
private void setAsRingtone(final Activity context, int position) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.System.canWrite(context)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + context.getPackageName()));
            context.startActivity(intent);
        } else {
//            List<musicInfo> list = MyApplication.getMusicInfoArrayList();
            File music = new File(musicInfoArrayList.get(position).getMusicData()); // path is a file to /sdcard/media/ringtone
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, music.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, musicInfoArrayList.get(position).getMusicTitle());
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.ARTIST, musicInfoArrayList.get(position).getMusicArtist());
            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 uri = MediaStore.Audio.Media.getContentUriForPath(music.getAbsolutePath());
            context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + music.getAbsolutePath() + "\"", null);
            Uri newUri = context.getContentResolver().insert(uri, values);
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
            Toast.makeText(context, "已成功设置为来电铃声", Toast.LENGTH_SHORT).show();
//            //Snackbar
//            Snackbar.make(rootview, "已成功设置为来电铃声", Snackbar.LENGTH_LONG).setAction("好的", new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                }
//            }).show();
        }
    }
 
Example 11
Source File: MediaStoreUtil.java    From APlayer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 设置铃声
 */
public static void setRing(Context context, int audioId) {
  try {
    ContentValues cv = new ContentValues();
    cv.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    cv.put(MediaStore.Audio.Media.IS_ALARM, false);
    cv.put(MediaStore.Audio.Media.IS_MUSIC, true);
    // 把需要设为铃声的歌曲更新铃声库
    if (mContext.getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cv,
        MediaStore.MediaColumns._ID + "=?", new String[]{audioId + ""}) > 0) {
      Uri newUri = ContentUris
          .withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, audioId);
      RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
      ToastUtil.show(context, R.string.set_ringtone_success);
    } else {
      ToastUtil.show(context, R.string.set_ringtone_error);
    }
  } catch (Exception e) {
    //没有权限
    if (e instanceof SecurityException) {
      ToastUtil.show(context, R.string.please_give_write_settings_permission);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.System.canWrite(mContext)) {
          Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
          intent.setData(Uri.parse("package:" + mContext.getPackageName()));
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          if (Util.isIntentAvailable(mContext, intent)) {
            mContext.startActivity(intent);
          }
        }
      }
    }

  }
}
 
Example 12
Source File: RingtoneSetting.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public static void setting(Context context) {
	// 外部调用者传来的context
	mContext = context;
	// 设置歌曲路径
	File filePath = new File(mUrl);
	ContentValues values = new ContentValues();
	// The data stream for the file
	values.put(MediaStore.MediaColumns.DATA, filePath.getAbsolutePath());
	// The title of the content
	values.put(MediaStore.MediaColumns.TITLE, filePath.getName());
	// The MIME type of the file
	values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
	// values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
	// values.put(MediaStore.Audio.Media.DURATION, 230);
	// 来电铃声
	// 第二个参数若是true则会在铃音库中显示
	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, true);
	// Insert it into the database
	Uri uri = MediaStore.Audio.Media.getContentUriForPath(filePath
			.getAbsolutePath());
	// 下面这一句很重要
	mContext.getContentResolver().delete(
			uri,
			MediaStore.MediaColumns.DATA + "=\""
					+ filePath.getAbsolutePath() + "\"", null);
	Uri newUri = mContext.getContentResolver().insert(uri, values);
	RingtoneManager.setActualDefaultRingtoneUri(mContext,
			RingtoneManager.TYPE_RINGTONE, newUri);

}
 
Example 13
Source File: RecentListAdapter.java    From music_player with Open Software License 3.0 5 votes vote down vote up
private void setAsRingtone(final Activity context, int position) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.System.canWrite(context)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + context.getPackageName()));
            context.startActivity(intent);
        } else {
            List<musicInfo> list = MyApplication.getDatesublist();
            File music = new File(list.get(position).getMusicData()); // path is a file to /sdcard/media/ringtone
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, music.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, list.get(position).getMusicTitle());
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.ARTIST, list.get(position).getMusicArtist());
            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 uri = MediaStore.Audio.Media.getContentUriForPath(music.getAbsolutePath());
            context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + music.getAbsolutePath() + "\"", null);
            Uri newUri = context.getContentResolver().insert(uri, values);
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
            Toast.makeText(context, "已成功设置为来电铃声", Toast.LENGTH_SHORT).show();
            //Snackbar
//            Snackbar.make(rootview, "已成功设置为来电铃声", Snackbar.LENGTH_LONG).setAction("好的", new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                }
//            }).show();
        }
    }
 
Example 14
Source File: PlayDetailActivity.java    From BeMusic with Apache License 2.0 5 votes vote down vote up
private void setRingtone () {
    Song song = PlayManager.getInstance(this).getCurrentSong();
    if (song != null) {
        RingtoneManager.setActualDefaultRingtoneUri(
                this,
                RingtoneManager.TYPE_RINGTONE,
                Uri.fromFile(new File(song.getPath())));
    }
}
 
Example 15
Source File: SynthCircle.java    From Circle-Synth with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Here we must copy the recording to be set as a ringtone into the user's
 * /Ringtones folder, and then create the MediaStore entry so that we can
 * proceed to set it as the user's default ringtone.
 */
public void setRingtone() {

	String path = Environment.getExternalStorageDirectory().toString()
			+ "/Ringtones";
	File k = new File(path, "CircleSynthRing.wav");
	ContentValues values = new ContentValues();
	values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
	values.put(MediaStore.MediaColumns.TITLE, "CircleSynthRingtone");
	values.put(MediaStore.MediaColumns.SIZE, k.length());
	values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/vnd.wave");
	values.put(MediaStore.Audio.Media.ARTIST, "CircleSynth");
	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);

	System.out.println("getting the right file "
			+ String.valueOf(k.length()));

	// Insert it into the database
	Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.toString());
	// delete previous entries - do this to avoid duplicate entries of the
	// same name
	getContentResolver().delete(
			uri,
			MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath()
					+ "\"", null);
	System.out.println("file in question " + k.getAbsolutePath());

	// insert new values into the DB
	Uri newUri = this.getContentResolver().insert(uri, values);

	// set as default ringtone
	try {
		RingtoneManager.setActualDefaultRingtoneUri(getBaseContext(),
				RingtoneManager.TYPE_RINGTONE, newUri);
	} catch (Throwable t) {
		Log.d(TAG, "catch exception");
		System.out.println("ringtone set exception " + t.getMessage());
	}

}
 
Example 16
Source File: MediaUtils.java    From freemp with Apache License 2.0 4 votes vote down vote up
public static void setRingtone(Context context, ClsTrack track) {

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, track.getPath());
        values.put(MediaStore.MediaColumns.TITLE, track.getTitle());
        //values.put(MediaStore.MediaColumns.SIZE, 1024*1024);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
        values.put(MediaStore.Audio.Media.ARTIST, track.getArtist());
        //values.put(MediaStore.Audio.Media.DURATION, 5000);
        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, true);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(track.getPath());

        if (uri == null || context.getContentResolver() == null) {
            Toast.makeText(context, context.getString(R.string.error), Toast.LENGTH_SHORT).show();
            return;
        }


        /*String ringTonePath = uri.toString();
        try {
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, uri);
        } catch (Exception e) {
            Toast.makeText(context, context.getString(R.string.error)+e.toString(), Toast.LENGTH_SHORT).show();
        }*/

        //TODO check this may be better copy file in ringtone dir before?
        context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + track.getPath() + "\"", null);
        Uri newUri = context.getContentResolver().insert(uri, values);

        if (newUri == null) {
            Toast.makeText(context, context.getString(R.string.error), Toast.LENGTH_SHORT).show();
        } else {
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
            Toast.makeText(context, context.getString(R.string.set_as_ringtone), Toast.LENGTH_SHORT).show();
        }

    }
 
Example 17
Source File: SoundUtil.java    From MVPAndroidBootstrap with Apache License 2.0 4 votes vote down vote up
/**
 * Sets default ringtone.
 * @param rawSoundId
 * @param ctx
 */
public static void setRawSoundRingTone(int rawSoundId, Context ctx) {
    Uri path = Uri.parse("android.resource://" + ctx.getPackageName() + "/raw/" + rawSoundId);
    RingtoneManager.setActualDefaultRingtoneUri(ctx, RingtoneManager.TYPE_RINGTONE, path);
    RingtoneManager.getRingtone(ctx, path).play();
}
 
Example 18
Source File: SoundUtil.java    From RxAndroidBootstrap with Apache License 2.0 4 votes vote down vote up
/**
 * Sets default ringtone.
 * @param rawSoundId
 * @param ctx
 */
public static void setRawSoundRingTone(int rawSoundId, Context ctx) {
    Uri path = Uri.parse("android.resource://" + ctx.getPackageName() + "/raw/" + rawSoundId);
    RingtoneManager.setActualDefaultRingtoneUri(ctx, RingtoneManager.TYPE_RINGTONE, path);
    RingtoneManager.getRingtone(ctx, path).play();
}
 
Example 19
Source File: AudioUtil.java    From VCL-Android with Apache License 2.0 4 votes vote down vote up
@RequiresPermission(android.Manifest.permission.WRITE_SETTINGS)
public static void setRingtone(MediaWrapper song, Context context){
    File newringtone = AndroidUtil.UriToFile(song.getUri());
    if(newringtone == null || !newringtone.exists()) {
        Toast.makeText(context.getApplicationContext(),context.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 {
        context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newringtone.getAbsolutePath() + "\"", null);
        newUri = context.getContentResolver().insert(uri, values);
        RingtoneManager.setActualDefaultRingtoneUri(
                context.getApplicationContext(),
                RingtoneManager.TYPE_RINGTONE,
                newUri
        );
    } catch(Exception e) {
        Toast.makeText(context.getApplicationContext(),
                context.getString(R.string.ringtone_error),
                Toast.LENGTH_SHORT).show();
        return;
    }

    Toast.makeText(
            context.getApplicationContext(),
            context.getString(R.string.ringtone_set, song.getTitle()),
            Toast.LENGTH_SHORT)
            .show();

}
 
Example 20
Source File: RingTools.java    From Mp3Cutter with GNU General Public License v3.0 votes vote down vote up
private static void setRingHasPermission(Activity context, int type, String path) {
        Uri oldRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE); //系统当前  通知铃声
        Uri oldNotification = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION); //系统当前  通知铃声
        Uri oldAlarm = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM); //系统当前  闹钟铃声
        File sdfile = new File(path);
        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/mp3");
        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, true);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());
        Uri newUri = null;
        String deleteId = "";
        try {
            Cursor cursor = context.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[]{path}, null);
            if (cursor.moveToFirst()) {
                deleteId = cursor.getString(cursor.getColumnIndex("_id"));
            }
            Logger.d(" + deleteId" + deleteId);

            context.getContentResolver().delete(uri,
                    MediaStore.MediaColumns.DATA + "=\"" + sdfile.getAbsolutePath() + "\"", null);
            newUri = context.getContentResolver().insert(uri, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (newUri != null) {
            String ringStoneId = "";
            String notificationId = "";
            String alarmId = "";
            if (null != oldRingtoneUri) {
                ringStoneId = oldRingtoneUri.getLastPathSegment();
            }
            if (null != oldNotification) {
                notificationId = oldNotification.getLastPathSegment();
            }
            if (null != oldAlarm) {
                alarmId = oldAlarm.getLastPathSegment();
            }
            Uri setRingStoneUri;
            Uri setNotificationUri;
            Uri setAlarmUri;
            if (type == RingtoneManager.TYPE_RINGTONE || ringStoneId.equals(deleteId)) {
                setRingStoneUri = newUri;
            } else {
                setRingStoneUri = oldRingtoneUri;
            }
            if (type == RingtoneManager.TYPE_NOTIFICATION || notificationId.equals(deleteId)) {
                setNotificationUri = newUri;
            } else {
                setNotificationUri = oldNotification;
            }
            if (type == RingtoneManager.TYPE_ALARM || alarmId.equals(deleteId)) {
                setAlarmUri = newUri;
            } else {
                setAlarmUri = oldAlarm;
            }
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, setRingStoneUri);
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION, setNotificationUri);
            RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM, setAlarmUri);
            switch (type) {
                case RingtoneManager.TYPE_RINGTONE:
                    Toast.makeText(context.getApplicationContext(), "设置来电铃声成功!", Toast.LENGTH_SHORT).show();
                    break;
                case RingtoneManager.TYPE_NOTIFICATION:
                    Toast.makeText(context.getApplicationContext(), "设置通知铃声成功!", Toast.LENGTH_SHORT).show();
                    break;
                case RingtoneManager.TYPE_ALARM:
                    Toast.makeText(context.getApplicationContext(), "设置闹钟铃声成功!", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }