2015-11-13 55 views
0

我試圖將這些數據從我的活動傳遞給用戶點擊歌曲時的接收器。無法將活動數據傳遞給BroadcastReceiver?

MyActivity

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

這是我在我的接收機類

接收機

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

我的應用程序崩潰寫道:當我點擊我的歌。

不知道這是正確的錯誤消息:

11-12 21:30:40.562 24747-24747/com.piersonleo.lockscreensound E/SecondScreen﹕ Intent error 
+0

請將錯誤消息添加到您的問題中,以便回答更容易。 – juniperi

+0

我添加了它,但我不知道它是否正確。 –

回答

0

首先,數據定義你的manifest.xml接收機

<receiver android:name="com.xxx.xxx.LockScreenReceiver" > 
     <intent-filter> 
      <action android:name="com.xxx.xxx.xxxx" /> 
     </intent-filter> 
    </receiver> 

其次,sendBroadcast這樣的:

Intent i = new Intent("com.xxx.xxx.xxxx"); 
intent.putExtra("pos", newPosition2).putExtra("songlist", mySongs).putExtra("lockSound", "lock"); 
sendBroadcast(i); 
相關問題