java – 使用Intents传递铃声数据

到目前为止,我已经通过为1个文件创建1个活动来创建具有设置为铃声功能的应用程序.这很糟糕,因为有超过20个铃声的应用程序,我需要20个活动,这将影响应用程序的大小和性能.然后我发现有一种方法可以只使用1个活动和布局,使用Intents传递数据.现在我很清楚它是如何工作的,除了困扰我的一件事.那就是我如何定义字符串.

我需要1个字符串用于名称,1个用于文件路径

我的代码:

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();

那我该怎么做?如何为每个文件定义字符串以及如何传递它们?

由于某些成员的问题不清楚,我会更简单
我不知道如何编写字符串,所以当我使用Intent启动RingtoneManager Activity时,我从字符串传递数据.
那么我应该如何编写代码来传递这个

文件名“Slow tone.mp3”

文件路径:R.raw.s8slowtone)

铃声名称“慢音”

最佳答案 要调用您的活动,只需将此功能放在任何位置,然后使用所需的参数调用它.它将构建一个intent并填写参数:

 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);
} 

在RingToneActivity的onCreate中,您只需检索刚刚传递的参数:

@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!

}

笔记:

>如果活动名称不同,则在函数中替换“RingToneActivity.class”.

点赞