为什么这不起作用?我有activityClass(PlayListActivity.java
)和serviceClass(AudioService.java
)。当我点击ListView
时,歌曲应该开始播放,但事实并非如此。当我点击ListView
时,它会关闭应用程序而不会出现任何错误(例如forced close
)。
我在LogCat中遇到了这个错误:
活动com.example.serviceaudio.PlayListActivity泄露了最初绑定的ServiceConnection com.example.serviceaudio.PlayListActivity$AudioPlayerServiceConnection@41b1c448
致命的例外:马宁 java.lang.RunTimeException:接收广播的错误Intent {act=com.example.serviceaudio.AudioService$AudioPlayerBroadCastResiever@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);
}
}
}
}
答案 0 :(得分:0)
问题很可能是您绑定到Service
但是当您点击列表项时,您正在调用finish()
,这将终止Activity
1}}。您的代码中没有任何内容可以显示您从Service
解除绑定。
您似乎不仅因为启动Service
而是绑定它并使用BroadcastReceiver
而混淆了这个问题。绑定到Service
的目的是允许(例如)Activity
和Service
之间的直接通信,在这种情况下,通过BroadcastReceiver
进行通信。必要的。
最重要的方面是,如果您绑定到Service
,请确保根据要求在onPause()
,onStop()
或onDestroy()
取消绑定。