繁体   English   中英

使用Intents传递铃声数据

[英]Using Intents to pass data for ringtones

到目前为止,我已经通过为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's 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”替换为活动名称。
 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);
         }

然后在Ringtone Manager中,在我的案例中RMpro

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

然后就是:

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

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

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

您可以通过意图传递整个对象。 您只需要为要传递的类实现Serializable接口。 例:

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

通过意图发送:

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

通过意图检索:

Ringtone ringtoneFromIntent = (Ringtone) intent.getSerializableExtra("test");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM