音乐播放,混合启动

import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.os.Bundle;import android.os.IBinder;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends Activity {MyConnection conn;private MyBinder musicContro1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent service = new Intent(this,MusicPlayerService.class);conn = new MyConnection();//第一步,调用通过bindService bind方式开启音乐播放的服务bindService(service, conn, BIND_AUTO_CREATE);}@Overrideprotected void onDestroy() {super.onDestroy();unbindService(conn);}//上一首void pre(View v){musicContro1.callpre();}//播放public void play(View v){musicContro1.callplay()}//暂停public void pause(View v){musicContro1.callpause()//下一首public void next(View v){musicContro1.callNext();}private class MyConnection implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {/第四步,Service执行完onBind之后 如果返回值不为空//就会走这个方法,参数IBinder对象就是onBind的返回值//获取到这个对象,就可以调用它的public方法musicContro1 = (MyBinder) service;}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}}}import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class MusicPlayerService extends  Service{@Overridepublic IBinder onBind(Intent intent) {//第三步 ,执行onBindreturn new MyBinder();}@Overrideublic void onCreate() {super.onCreate();//第二步,执行onCreate//MediaPlayerLog.e("TAG", "音乐播放器");}@Overridepublic void onDestroy() {super.onDestroy();Log.e("TAG", "onDestroy")}private void next(){Log.e("TAG", "播放下一首----甜蜜蜜");}private void pre(){Log.e("TAG", "播放上一首----一剪梅");}private void play(){Log.e("TAG", "播放----倩女幽魂");}private void pause() {Log.e("TAG", "暂停");}public class   MyBinder extends Binder{public void callNext(){next();}public void callpre(){pre();}public void callplay(){play();}public void callpause(){pause();}}}activity--xml文件android:layout_width="fill_parent"android:layout_height="fill_parent" >="wrap_content"android:layout_height="wrap_content"android:text="上一首"android:onClick="pre" />="wrap_content"android:layout_height="wrap_content"android:text="放"android:onClick="play"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="暂停"android:onClick="pause" />android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="下一首"android:onClick="next" />public class MainActivity extends Activity {Mycoment ic;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void bb1(View v){startService(new Intent(MainActivity.this,Serci.class));}public void bb2(View v){stopService(new Intent(this,Serci.class));}public void bb3(View v){Intent in = new Intent(this,Serci.class);ic = new Mycoment();bindService(in, ic, BIND_AUTO_CREATE);startService(in);}public void bb4(View v){(ic);}public class Mycoment implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stub}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}}}class Serci extends Service{@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn new Binderl();}@Overridepublic void onCreate() {/ TODO Auto-generated method stubsuper.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubreturn super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy()}public class Binderl extends Binder{}}

你可能感兴趣的:(音乐播放,混合启动)