android使用service

运行效果:一开始app调用service播放音乐,点击左上角的音量按钮会停止播放音乐。

          

结构目录图:

 

 

activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:background="@drawable/bg"
 8     tools:context=".MainActivity">
 9 
10     <ImageButton
11         android:id="@+id/btn_play"
12         android:src="@drawable/play"
13         android:layout_width="wrap_content"
14         android: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
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main);
15 
16         final Intent intent = new Intent(MainActivity.this, MusicService.class);
17 
18         ImageButton btn_play = findViewById(R.id.btn_play); //获取“播放/停止”按钮
19         //启动service与停止service,实现播放背景音乐与停止播放背景音乐
20         btn_play.setOnClickListener(new View.OnClickListener() {
21             @Override
22             public void onClick(View v) {
23                 if (MusicService.isplay == false) { //判断音乐播放状态
24                     //启动service,从而实现播放背景音乐
25                     startService(intent);
26                     //更换播放背景音乐图标
27                     ((ImageButton) v).setImageDrawable(getResources().getDrawable(R.drawable.play, null));
28                 } else {
29                     //停止service,从而实现停止播放背景音乐
30                     stopService(intent);
31                     //更换停止背景音乐图标
32                     ((ImageButton) v).setImageDrawable(getResources().getDrawable(R.drawable.stop, null));
33                 }
34             }
35         });
36     }
37 
38     @Override
39     protected void onStart() {  //实现进入界面时,启动背景音乐service
40         //启动service,从而实现播放背景音乐
41         startService(new Intent(MainActivity.this, MusicService.class));
42 
43         super.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 {
 9     static boolean isplay;  //定义当前播放状态
10     MediaPlayer player; //MediaPlayer对象
11 
12     public MusicService() {
13     }
14 
15     @Override
16     public IBinder onBind(Intent intent) {  //必须实现的绑定方法
17         // TODO: Return the communication channel to the service.
18         throw new UnsupportedOperationException("Not yet implemented");
19     }
20 
21     @Override
22     public void onCreate() {    //创建MediaPlayer对象并加载播放的音乐文件
23         player = MediaPlayer.create(this, R.raw.music);
24 
25         super.onCreate();
26     }
27 
28     @Override
29     public int onStartCommand(Intent intent, int flags, int startId) {
30         if (!player.isPlaying()) {  //如果没有播放音乐
31             player.start(); //播放音乐
32             isplay = player.isPlaying();    //当前状态正在播放音乐
33         }
34 
35         return super.onStartCommand(intent, flags, startId);
36     }
37 
38     @Override
39     public void onDestroy() {   //停止音乐的播放
40         player.stop();  //停止音频的播放
41         isplay = player.isPlaying();    //当前状态没有播放音乐
42         player.release();   //释放资源
43 
44         super.onDestroy();
45     }
46 }

 

 

manifests:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.mingrisoft.serveicmusicplayer">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:roundIcon="@mipmap/ic_launcher_round"
10         android:supportsRtl="true"
11         android: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
21             android:name=".MusicService"
22             android:enabled="true"
23             android:exported="true"></service>
24     </application>
25 
26 </manifest>

 

posted @ 2020-04-04 22:10  hemeiwolong  阅读(154)  评论(0编辑  收藏  举报