質問

So far I've been making applications with set as Ringtone feature by creating 1 activity for 1 file. It was bad because with apps with more than 20 ringtones I would've needed 20 activities which would affect app size and performance. Then I found that there is a way to do that with only 1 activity and layout, passing data with Intents. Now I have pretty good idea how that works except one thing that bothers me. That is how do I define strings. I need 1 string for name and 1 for file path

My code:

Boolean success = false;
                    rsound = new File(rpath, "Slow tone.mp3");rpath.mkdirs(); //Copied file name
                    if (!rsound.exists()) {




                        try {
                            InputStream in = getResources().openRawResource(R.raw.s8slowtone); //path for file 
                            FileOutputStream out = new FileOutputStream(rsound.getPath());
                            byte[] buff = new byte[1024];
                            int read = 0;

                            try {
                                while ((read = in.read(buff)) > 0) {
                                    out.write(buff, 0, read);
                                }
                            } finally {
                                in.close();

                                out.close();
                            }
                        } catch (Exception e) {
                            success = false;

                        }
                    } else {
                        success = true;
                        setRingtone();

                    }

                    if (!success) { 
                       setRingtone();


                    }
                }

                private void setRingtone() {
                    ContentValues values = new ContentValues();
                       values.put(MediaStore.MediaColumns.DATA, rsound.getAbsolutePath());
                       values.put(MediaStore.MediaColumns.TITLE, "Slow tone"); //Ringtone name
                       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
                       values.put(MediaStore.Audio.Media.ARTIST, " ");
                       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(rsound.getAbsolutePath());
                       getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + rsound.getAbsolutePath() + "\"",
                               null);
                       Uri newUri = getContentResolver().insert(uri, values);

                       RingtoneManager.setActualDefaultRingtoneUri(
                               S15.this, RingtoneManager.TYPE_RINGTONE,
                               newUri);
                       Toast.makeText(getApplicationContext(), "Ringtone set successfully",
                               Toast.LENGTH_SHORT).show();

So How do I do this? How do I define string for each file and how to pass them?

Since question is unclear for some members I will make it simpler I don't have idea how should I write strings so when I start RingtoneManager Activity using Intent, I pass data from strings. So How should I write my code to pass this

File name "Slow tone.mp3"

File path: R.raw.s8slowtone)

Ringtone name "Slow tone"

役に立ちましたか?

解決 2

 Intent f27=new Intent(context, RMpro.class);
         if (f27 != null){
         f27.putExtra("FileName", "Horn!"); //Copied file name
         int res = R.raw.s28horn; // Path to File in App ressources
         f27.putExtra("FilePath", res); //Passing path with intent
         f27.putExtra("RingName", "Horn.mp3"); // Ring name
         ((Activity)context).startActivity(f27);
         }

And then in Ringtone Manager, in my case RMpro

 final int FPATH=i.getExtras().getInt("FilePath");
       final  String RNAME = getIntent().getStringExtra("RingName").trim();
       final  String FNAME = getIntent().getStringExtra("FileName").trim();

And then just:

rsound = new File(rpath, FNAME);rpath.mkdirs();

InputStream in = getResources().openRawResource(FPATH);

values.put(MediaStore.MediaColumns.TITLE, RNAME);

他のヒント

To call your activity, just place this function anywhere, and call it with your desired parameters. It will build an intent and fill in the parameters:

 public static void runRingToneActivity(Context context, String ringToneName, String ringTonePath, String ringToneFilename) {
    Intent intent=new Intent(context, RingToneActivity.class);
    intent.putExtra("NAME", ringToneName);
    intent.putExtra("PATH", ringTonePath);
    intent.putExtra("FILE", ringToneFileName);
    ((Activity)context).startActivity(intent);
} 

Inside your RingToneActivity's onCreate, you just retrieve the parameters you just passed:

@Override
protected void onCreate(Bundle savedInstanceState) {

           . 
           .


    Intent intent=this.getIntent();

    String ringtoneName=intent.getStringExtra("NAME");
    String ringtonePath=intent.getStringExtra("PATH");
    String ringtoneFile=intent.getStringExtra("FILE");

            // you have them, now use them!

}

NOTES:

  • Substitute "RingToneActivity.class" in the function for the name of your activity, if it is different.

You can pass entire objects via intent. You just need to implement Serializable interface for the class you want to pass. Example:

public class Ringtone implements Serializable
{
    public String name;
    public String path; //can be integer
    public String file;
}

send via intent:

Ringtone ringtoneObj = new Ringtone();
intent.putExtra("test",ringtoneObj);

retrieve via intent:

Ringtone ringtoneFromIntent = (Ringtone) intent.getSerializableExtra("test");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top