【问题标题】:Pass data from first activity to second activity to play song将数据从第一个活动传递到第二个活动以播放歌曲
【发布时间】:2015-11-19 00:26:14
【问题描述】:

如何在这些活动之间传递数据,因为现在我可以在单击列表项时在第一个活动中播放音乐,但是现在我想将包含列表的第一个活动的数据传递给第二个活动,直接在第二个活动中播放歌曲我也对服务感到困惑,因为当我在第二个活动中播放歌曲时,我在第一个活动中使用服务,服务会发生什么

【问题讨论】:

  • 请帮忙回答我的问题

标签: android android-layout android-fragments android-intent android-activity


【解决方案1】:

您可以向启动第二个活动的意图添加额外数据。

为此,您的歌曲类必须实现 Parcelable

public class Song implements Parcelable
{
    string artist;
    string songName;
    string filePath;

    @Override
    public int describeContents()
    {
        return 0;
    }

    protected Song(Parcel in)
    {
        artist = in.readString();
        songName = in.readString();
        filePath = in.readString();     
    }

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
        dest.writeString(artist);
        dest.writeString(songName);       
        dest.writeString(filePath);
    }
}

然后当打开第二个活动来播放歌曲时,它会是这样的:

Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("song", songToPlay);
getApplicationContext().startActivity(intent);

最后,您将在第二个活动中收到如下歌曲:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.song_player);

    Intent intent = getIntent();
    if (intent != null)
    {
        if (intent.hasExtra("song"))
        {
            Song songToPlay = intent.getExtra("song");      
        }
    }
}

【讨论】:

  • 其实我用的是这个但是数组里面的数据是两个很多
  • 如果必须传递文件,只需传递文件位置
  • 实际上我使用了内容提供者,然后如果我从第一个活动传递数据的 id,然后在第二个活动中,如果我查询内容提供者以获取相同的 id 数据,我现在将其放入列表中,我该怎么做那
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 2020-01-13
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
相关资源
最近更新 更多