Activity與Service之間交互並播放歌曲,為了方便,我把要播放的歌曲定死了,大家可以靈活改進
MService:
MusicPlayActivity:
package com.tiantian.test;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class MusicPlayActivity extends Activity {
/** Called when the activity is first created. */
MService mService;
private ServiceConnection conn = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
mService = ((MService.MyBinder)arg1).getService();
Log.v("CAT", "getServiced");
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mService = null;
}
};
private SeekBar seekBar;
private Button playBT;
private boolean isPlaying = false;
private boolean isBinded = false;
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(MusicPlayActivity.this, MService.class);
if(!isBinded){
bindService(intent, conn, BIND_AUTO_CREATE);
isBinded = true;
}
&