【问题标题】:Using Intents to pass data for ringtones使用 Intent 为铃声传递数据
【发布时间】:2014-04-05 16:08:23
【问题描述】:

到目前为止,我一直在通过为 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 时,我会从字符串中传递数据。 那么我应该如何编写我的代码来传递这个

文件名“慢音.mp3”

文件路径:R.raw.s8slowtone)

铃声名称“慢音”

【问题讨论】:

  • 我不明白你需要什么,是活动的代码设置了铃声吗?您想从意图中获取文件名和名称吗?
  • 我想知道应该如何编写字符串,以便在开始新活动时传递它们。
  • "我应该如何写字符串" => 你能说得更具体点吗?您使用 " " 创建字符串
  • 我应该如何制作字符串,这样当我传递它时它就有路径、文件名和铃声名称。
  • 在这种情况下,您不会“创建”String,而是从某个地方获得它......资源?您的铃声存储在哪里?

标签: java android string android-intent ringtone


【解决方案1】:

要调用您的活动,只需将此函数放在任何位置,并使用所需的参数调用它。它将建立一个意图并填写参数:

 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”。

【讨论】:

  • 我试过你的代码,它给了我很多不合逻辑的错误Multiple markers at this line - Syntax error, insert ";" to complete LocalVariableDeclarationStatement - Syntax error on token "(", ; expected - Syntax error on token ",", ; expected - Syntax error on token ",", ; expected
  • “。”表示您的活动中有更多内容 xDD
【解决方案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);
         }

然后在铃声管理器中,在我的情况下为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);

【讨论】:

    【解决方案3】:

    您可以通过意图传递整个对象。您只需要为要传递的类实现 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");
    

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 2012-12-29
      • 1970-01-01
      • 2013-01-13
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 2018-05-24
      相关资源
      最近更新 更多