1.在Android系统开发之中,Services是一个重要的组成部分。Services实际上就相当于是一个没有图形界面的Activity程 序,
而且当用户要执行某些操作需要进行跨进程访问的时候 ,也可以使用Service来完成。
2.Service是一个没有UI界面的操作组件
3.继承android.app.Service类实现
4.Service启动有2中 Context.startService() 和 Context.bindService();
5. 使用context.startService() 启动Service是会会经历:
context.startService() ->onCreate()- >onStartCommand()->Service running
context.stopService() | ->onDestroy() ->Service stop
一、如果Service还未运行:则调用onCreate()- >onStartCommand();
二、如果Service已经运行:则只调用onStartCommand();
三、如果调用者自己退出而没有调用stopService的话,Service会一直在后台运行。
四、所以调用startService的生命周期为:onCreate()--> onStartCommand()(可多次调用) --> onDestroy()
6. 使用使用context.bindService()启动Service会经历:
context.bindService()->onCreate()->onBind()->Service running
onUnbind() ->onDestroy() ->Service stop
一、调用者会和Service绑定在一起,调用者退出了,Srevice就会调用onUnbind->onDestroy相应退出。
二、所以调用bindService的生命周期为:onCreate --> onBind(只一次,不可多次绑定) --> onUnbind --> onDestory。
7.新建一个类继承Service
package com.zdsft.servicetest.service; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import com.zdsft.servicetest.R; import java.io.IOException; /** * Created by Administrator on 2017/3/22. */ public class MyServiceMusicTest extends Service { MediaPlayer mediaPlayer; public void onCreate() { super.onCreate(); mediaPlayer = MediaPlayer.create(this, R.raw.cqtx); //设置是否循环 mediaPlayer.setLooping(true); } public int onStartCommand(Intent intent, int flags, int startId) { int a = intent.getIntExtra("code",1); switch (a){ case 1: closeMusic(); break; case 2: startMusic(); break; case 3: puseMusic(); break; } return super.onStartCommand(intent, flags, startId); } public void onDestroy() { super.onDestroy(); } public IBinder onBind(Intent intent) { return null; } //开始音乐 private void startMusic(){ if (mediaPlayer!=null){ mediaPlayer.start(); } } //暂停播放 private void puseMusic(){ if (mediaPlayer!=null&&mediaPlayer.isPlaying()){ mediaPlayer.pause(); } } //关闭播放 private void closeMusic(){ if (mediaPlayer!=null){ mediaPlayer.stop(); try { mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.seekTo(0); } } } 写完之后要在AndroidManifest.xml 中注册,要在res/下新建一个raw包里面放一个MP3格式的歌曲
<service android:name=".service.MyServiceMusicTest"></service>
在写一个测试类
package com.zdsft.servicetest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.zdsft.servicetest.service.MyServiceMusicTest;
public class MainActivity extends Activity implements View.OnClickListener{ Button puse_btn,play_btn,close_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } //初始化 private void init() { close_btn = (Button) findViewById(R.id.end_btn); play_btn = (Button) findViewById(R.id.play_btn); puse_btn = (Button) findViewById(R.id.puse_btn); close_btn.setOnClickListener(this); play_btn.setOnClickListener(this); puse_btn.setOnClickListener(this); } public void onClick(View view) { switch (view.getId()){ case R.id.end_btn: playService(1); break; case R.id.play_btn: playService(2); break; case R.id.puse_btn: playService(3); break; } } //开启服务的方法 private void playService(int code){ Intent intent = new Intent(this, MyServiceMusicTest.class); intent.putExtra("code",code); startService(intent); } }
测试类的xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/puse_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="暂停"/> <Button android:id="@+id/play_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="播放"/> <Button android:id="@+id/end_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="结束"/> </LinearLayout>
这样,测试的时候,点击按钮能实现歌曲的播放,暂停,和结束,效果如图