当用户单击歌曲时,我试图将这些数据从我的活动传递给我的接收器。
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发布于 2015-11-13 02:33:39
首先,在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);https://stackoverflow.com/questions/33684698
复制相似问题