Search code examples
androidlistviewonitemclicklistenerringtone

The method setActualDefaultRingtoneUri(Context, int, Uri) in the type RingtoneManager is not applicable for the arguments OnItemClickListener


I am making android application and I want when I click on list view item to find ID of clicked item, get resources and set that file as ringtone.However I get this error : The method setActualDefaultRingtoneUri(Context, int, Uri) in the type RingtoneManager is not applicable for the arguments (new AdapterView.OnItemClickListener)

How can I fix this?

listv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        String selectedName = ((ListView) view.findViewById(R.id.listView1)).getContext()
                .toString();

        AssetManager assetManager = getAssets();

        File file = new File(Environment.getExternalStorageDirectory(),
                "/myRingtonFolder/Audio/");
        if (!file.exists()) {
            file.mkdirs();
        }

        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/MyRingtones/Audio/";

        File out = new File(path + "/", selectedName);
        if (!out.exists()) {
            try {
                copyFile(assetManager, "Ringtone.mp3", out);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, out.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, "airh");
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.MediaColumns.SIZE, out.length());
        values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
        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(out.getAbsolutePath());
        ContentResolver mCr = getContentResolver();
        Uri newUri = mCr.insert(uri, values);

        try {
            RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE,
                    newUri);
            Settings.System.putString(mCr, Settings.System.RINGTONE,
                    newUri.toString());
        } catch (Throwable t) {
            //TODO Handle exception
        }
        switch (position) {
            case 0:
                if (mp != null) {
                    mp.release();
                    mp = null;
                }

                mp = MediaPlayer.create(Context.this,
                        R.raw.aint);
                mp.start();

                break;
            case 1:
                if (mp != null) {
                    mp.release();
                    mp = null;
                }

                mp = MediaPlayer.create(Context.this,
                        R.raw.airh);
                mp.start();
                break;
        }
    }
});

Solution

  • You are within the OnItemClickListener interface so this will referrence to it.

    To avoid this just place the Name of your Activity before this or ask getApplicationContext() for context

    Like:

    RingtoneManager.setActualDefaultRingtoneUri(
         MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
    

    or

    RingtoneManager.setActualDefaultRingtoneUri(
         getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);
    

    do the same for MediaPlayer.create(MainActivity.this, R.raw.aint);