1 回答
TA贡献1831条经验 获得超4个赞
根本原因: sendBroadcast是Context类的方法,因为你在Adapter类中调用它,所以编译器显示错误“sendBroadcast 无法解析”。
解决方案:从视图实例中获取上下文,然后调用sendBroadcast方法。
holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Store songList and songIndex in mSharedPreferences
storageUtil.storeSong(Main.musicList);
storageUtil.storeSongIndex(holder.getAdapterPosition());
// Obtain context from view instance.
Context context = v.getContext();
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
context.sendBroadcast(broadCastReceiverIntent);
Intent broadCastReceiverIntentUpdateSong = new Intent(Constants.ACTIONS.BROADCAST_UPDATE_SONG);
context.sendBroadcast(broadCastReceiverIntentUpdateSong);
}
});
添加回答
举报