为什么这不起作用?我有activityClass(PlayListActivity.java
)和一个serviceClass(AudioService.java
)。当我点击ListView
时,歌曲应该开始播放,但这不是正在发生。当我点击ListView
时,它会关闭没有错误的应用程序(例如forced close
)。BroadCast接收器错误
我得到这个错误的logcat:
活动com.example.serviceaudio.PlayListActivity已经泄露,最初这里必然
致命异常ServiceConnection com.exa[email protected]41b1c448:马宁 了java.lang.RuntimeException:错误recieving广播意向{act=com[email protected]41bdf88
这里是我的代码:
PlayListActivity.java
public class PlayListActivity extends ListActivity {
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private ServiceConnection serviceConnection = new AudioPlayerServiceConnection();
private AudioService audioPlayer;
private Intent audioPlayerIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
audioPlayerIntent = new Intent(this, AudioService.class);
bindService(audioPlayerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongManager plm = new SongManager();
this.songsList = plm.getPlayList(this);
for (int i = 0; i < songsList.size(); i++) {
HashMap<String, String> song = songsList.get(i);
songsListData.add(song);
}// end loop
ListAdapter adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, new String[] { "songTitle","songArtist"}, new int[] {
R.id.songTitle, R.id.songArtist });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
playSong(position);
finish();
}
});
}
private final class AudioPlayerServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName className, IBinder baBinder) {
audioPlayer = ((AudioService.AudioPlayerBinder) baBinder).getService();
startService(audioPlayerIntent);
}
public void onServiceDisconnected(ComponentName className) {
audioPlayer = null;
}
}
public void playSong(int songIndex){
Intent intent = new Intent(AudioService.PLAY_SONG);
intent.putExtra("songIndex", songIndex);
this.sendBroadcast(intent);
}
}
AudioService.java
public class AudioService extends Service implements OnCompletionListener{
public static final String PLAY_SONG_BASE = "com.example.serviceaudio.AudioService";
public static final String PLAY_SONG = PLAY_SONG_BASE + ".PLAY_SONG";
private ArrayList<HashMap<String, String >> songList = new ArrayList<HashMap<String, String>>();
private SongManager songManager = new SongManager();
private MediaPlayer mp;
private AudioPlayerBroadcastReceiver broadcastReceiver = new AudioPlayerBroadcastReceiver();
public int currentSongIndex;
public class AudioPlayerBinder extends Binder {
public AudioService getService() {
return AudioService.this;
}
}
private final IBinder audioPlayerBinder = new AudioPlayerBinder();
@Override
public IBinder onBind(Intent intent) {
return audioPlayerBinder;
}
@Override
public void onCreate() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(PLAY_SONG);
registerReceiver(broadcastReceiver, intentFilter);
}
@Override
public void onStart(Intent intent, int startId) {
}
@Override
public void onDestroy() {
//release();
}
@Override
public void onLowMemory() {
stopSelf();
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
private void release() {
if(mp == null) {
return;
}
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
mp = null;
}
//==== playSong() ========
public void playSong(int songIndex){
// Play song
songList = songManager.getPlayList(this);
try {
mp.reset();
mp.setDataSource(songList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songList.get(songIndex).get("songTitle");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}// == End of playSong() ===
private class AudioPlayerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
currentSongIndex = intent.getExtras().getInt("songIndex");
if(PLAY_SONG.equals(action)) {
playSong(currentSongIndex);
}
}
}
}
你是什么意思,通过广播回放进行沟通? –
首先,当你调用'startService(...)'时,你传递一个'Intent',它可以包含一个额外的歌曲路径或URL等。另外一旦一个Service被绑定,Activity可以直接通过活页夹调用“服务”方法。我建议你仔细阅读这个... http://developer.android.com/guide/components/services.html – Squonk
我做了你所说的,我在'onDestroy()'中'解除了'服务',但我git this error致命异常:主要 com.example.serviceaudio.AudioService中的java.lang.RunTimeException:错误接收广播意图{act-com.exaple.serviceaudio.AudioService.PLAY_SONG flg = 0x10(有额外)}} AudioPlayerBroadcastReciever @ 41b4e818 –