我试图在用户点击一首歌时将这些数据从我的活动传递到我的接收器。
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 :(得分: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);