Search code examples
androidandroid-mediaplayerandroid-sound

Specific way to detect custom notification sounds in Android


I am currently offering a functionality for my app for which I display all the available system notification sounds to the user.

Now I want to detect the custom notification sounds that may be there in internal memory as well as SD card.

Currently I am thinking of getting a list of all available audio files and then filtering them by their duration (since notification sounds are very short in length).

Is there a better approach to detect custom notification sounds on the device? Answers with corresponding code would be highly appreciated.


Solution

  • RingtoneManager.getCursor() gives you a method to get all ringtones. Look below code of RingtoneManager.getCursor().

    /**
    364     * Returns a {@link Cursor} of all the ringtones available. The returned
    365     * cursor will be the same cursor returned each time this method is called,
    366     * so do not {@link Cursor#close()} the cursor. The cursor can be
    367     * {@link Cursor#deactivate()} safely.
    368     * <p>
    369     * If {@link RingtoneManager#RingtoneManager(Activity)} was not used, the
    370     * caller should manage the returned cursor through its activity's life
    371     * cycle to prevent leaking the cursor.
    372     * <p>
    373     * Note that the list of ringtones available will differ depending on whether the caller
    374     * has the {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission.
    375     *
    376     * @return A {@link Cursor} of all the ringtones available.
    377     * @see #ID_COLUMN_INDEX
    378     * @see #TITLE_COLUMN_INDEX
    379     * @see #URI_COLUMN_INDEX
    380     */
    381    public Cursor getCursor() {
    382        if (mCursor != null && mCursor.requery()) {
    383            return mCursor;
    384        }
    385
    386        final Cursor internalCursor = getInternalRingtones();
    387        final Cursor mediaCursor = getMediaRingtones();
    388
    389        return mCursor = new SortCursor(new Cursor[] { internalCursor, mediaCursor },
    390                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    391    }