android使用service

农村四月闲人少,勤学苦攻把名扬。这篇文章主要讲述android使用service相关的知识,希望能为你提供帮助。
运行效果:一开始app调用service播放音乐,点击左上角的音量按钮会停止播放音乐。
   

android使用service

文章图片
       
android使用service

文章图片

结构目录图:
android使用service

文章图片

 
 
activity_main.xml:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7android:background="@drawable/bg" 8tools:context=".MainActivity"> 9 10< ImageButton 11android:id="@+id/btn_play" 12android:src="https://www.songbingjia.com/android/@drawable/play" 13android:layout_width="wrap_content" 14android:layout_height="wrap_content" /> 15 16 < /RelativeLayout>

 
MainActivity:

1 package com.mingrisoft.serveicmusicplayer; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.ImageButton; 8 9 public class MainActivity extends AppCompatActivity { 10 11@Override 12protected void onCreate(Bundle savedInstanceState) { 13super.onCreate(savedInstanceState); 14setContentView(R.layout.activity_main); 15 16final Intent intent = new Intent(MainActivity.this, MusicService.class); 17 18ImageButton btn_play = findViewById(R.id.btn_play); //获取“播放/停止”按钮 19//启动service与停止service,实现播放背景音乐与停止播放背景音乐 20btn_play.setOnClickListener(new View.OnClickListener() { 21@Override 22public void onClick(View v) { 23if (MusicService.isplay == false) { //判断音乐播放状态 24//启动service,从而实现播放背景音乐 25startService(intent); 26//更换播放背景音乐图标 27((ImageButton) v).setImageDrawable(getResources().getDrawable(R.drawable.play, null)); 28} else { 29//停止service,从而实现停止播放背景音乐 30stopService(intent); 31//更换停止背景音乐图标 32((ImageButton) v).setImageDrawable(getResources().getDrawable(R.drawable.stop, null)); 33} 34} 35}); 36} 37 38@Override 39protected void onStart() {//实现进入界面时,启动背景音乐service 40//启动service,从而实现播放背景音乐 41startService(new Intent(MainActivity.this, MusicService.class)); 42 43super.onStart(); 44} 45 }

 
 
MusicService:

1 package com.mingrisoft.serveicmusicplayer; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.media.MediaPlayer; 6 import android.os.IBinder; 7 8 public class MusicService extends Service { 9static boolean isplay; //定义当前播放状态 10MediaPlayer player; //MediaPlayer对象 11 12public MusicService() { 13} 14 15@Override 16public IBinder onBind(Intent intent) {//必须实现的绑定方法 17// TODO: Return the communication channel to the service. 18throw new UnsupportedOperationException("Not yet implemented"); 19} 20 21@Override 22public void onCreate() {//创建MediaPlayer对象并加载播放的音乐文件 23player = MediaPlayer.create(this, R.raw.music); 24 25super.onCreate(); 26} 27 28@Override 29public int onStartCommand(Intent intent, int flags, int startId) { 30if (!player.isPlaying()) {//如果没有播放音乐 31player.start(); //播放音乐 32isplay = player.isPlaying(); //当前状态正在播放音乐 33} 34 35return super.onStartCommand(intent, flags, startId); 36} 37 38@Override 39public void onDestroy() {//停止音乐的播放 40player.stop(); //停止音频的播放 41isplay = player.isPlaying(); //当前状态没有播放音乐 42player.release(); //释放资源 43 44super.onDestroy(); 45} 46 }

 
 
manifests:

1 < ?xml version="1.0" encoding="utf-8"?> 2 < manifest xmlns:android="http://schemas.android.com/apk/res/android" 3package="com.mingrisoft.serveicmusicplayer"> 4 5< application 6android:allowBackup="true" 7android:icon="@mipmap/ic_launcher" 8android:label="@string/app_name" 9android:roundIcon="@mipmap/ic_launcher_round" 10android:supportsRtl="true" 11android:theme="@style/AppTheme"> 12< activity android:name=".MainActivity"> 13< intent-filter> 14< action android:name="android.intent.action.MAIN" /> 15 16< category android:name="android.intent.category.LAUNCHER" /> 17< /intent-filter> 18< /activity> 19 20< service 21android:name=".MusicService" 22android:enabled="true" 23android:exported="true"> < /service> 24< /application> 25 26 < /manifest>

【android使用service】 

    推荐阅读