首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能将数据从活动传递到BroadcastReceiver?

不能将数据从活动传递到BroadcastReceiver?
EN

Stack Overflow用户
提问于 2015-11-13 01:52:51
回答 1查看 35关注 0票数 0

当用户单击歌曲时,我试图将这些数据从我的活动传递给我的接收器。

MyActivity

代码语言:javascript
复制
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Other codes
                  try {
                    Intent intent = new Intent(getApplicationContext(), LockScreenReceiver.class);
                    intent.putExtra("pos", newPosition2).putExtra("songlist", mySongs).putExtra("lockSound", "lock");
                    startActivity(intent);
                }catch (Exception e) {
                    Log.e(TAG, "Intent error");
                }
}

这就是我在我的接受者课上写的

接收器

代码语言:javascript
复制
   public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    Bundle b = intent.getExtras();
    mySongs = (ArrayList) b.getParcelableArrayList("songlist");
    int position = b.getInt("pos", 0);

    Uri u = Uri.parse(mySongs.get(position).toString());
    mp = MediaPlayer.create(context, u);

    lockMusic = b.getString("lockSound");


    if (action.equals(Intent.ACTION_SCREEN_ON))
    {
        if(lockMusic!=null){
        mp.start();
        }
    }
 }

当我点击我的歌曲时,我的应用程序就崩溃了。

不确定这是否是正确的错误消息:

代码语言:javascript
复制
 11-12 21:30:40.562  24747-24747/com.piersonleo.lockscreensound E/SecondScreen﹕ Intent error
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-13 02:33:39

首先,在manifest.xml中定义接收器

代码语言:javascript
复制
   <receiver android:name="com.xxx.xxx.LockScreenReceiver" >
        <intent-filter>
            <action android:name="com.xxx.xxx.xxxx" />
        </intent-filter>
    </receiver>

其次,使用这样的数据的sendBroadcast:

代码语言:javascript
复制
Intent i = new Intent("com.xxx.xxx.xxxx");
intent.putExtra("pos", newPosition2).putExtra("songlist",   mySongs).putExtra("lockSound", "lock");
sendBroadcast(i);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33684698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档