<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/start_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="播放音乐"/> <Button android:id="@+id/stop_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="Stop"/>
</LinearLayout>
package com.example.music; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity { private Intent mIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIntent = new Intent(this,MyService.class); } public void onClick(View view) { switch (view.getId()){ case R.id.start_service: mIntent.putExtra("int",1); startService(mIntent); break; case R.id.stop_service: mIntent.putExtra("int",2); startService(mIntent); break; } } }
package com.example.music; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; /** * Created by 帅比浩宇 on 2017/12/13. * Service启动方式有两种 * startService()和bindService() * startService的生命周期 onCreate() onStartCommnud() onDestry() * binService生命周期 onBind() unonBind() */ public class Main2Activity extends AppCompatActivity { private ServiceConnection sc ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent = new Intent(this,MyBindService.class); sc = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { } @Override public void onServiceDisconnected(ComponentName componentName) { } }; bindService(intent,sc,BIND_AUTO_CREATE);//Activity 绑定一个服务 } @Override protected void onDestroy() { super.onDestroy(); unbindService(sc); } }
package com.example.music; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; /** * Created by 帅比浩宇 on 2017/12/13. */ public class MyBindService extends Service { //绑定服务 @Nullable @Override public IBinder onBind(Intent intent) { return null; } //解除绑定 @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } }
package com.example.music; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; /** * Created by 帅比浩宇 on 2017/12/13. */ public class MyService extends Service { private MediaPlayer mplayer; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d("sxl", "onCreate: "); mplayer = MediaPlayer.create(this,R.raw.a); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("sxl", "onStartCommand: "); int i = intent.getIntExtra("int",-1); switch (i){ case 1: musicStart(); break; case 2: stopMusic(); break; // case 3: // stopMusic(); // break; } return super.onStartCommand(intent, flags, startId); } //暂停播放 private void pauseMusic() { if(mplayer!=null && mplayer.isPlaying()){ mplayer.pause();//暂停播放 } } //停止播放 private void stopMusic(){ if(mplayer!=null && mplayer.isPlaying()){ mplayer.stop(); } } //开始播放 private void musicStart() { if(!mplayer.isPlaying() && mplayer!=null){ mplayer.start();//开始播放 } } }