加入收藏 | 设为首页 | 会员中心 | 我要投稿 南通站长网 (https://www.0513zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Android Service使用技巧--简单音乐播放实例

发布时间:2021-11-25 21:20:44 所属栏目:教程 来源:互联网
导读:Service翻译成中文是服务,熟悉Windows 系统的同学一定很熟悉了。Android里的Service跟Windows里的Service功能差不多,就是一个不可见的进程在后台执行。 Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果

Service翻译成中文是服务,熟悉Windows 系统的同学一定很熟悉了。Android里的Service跟Windows里的Service功能差不多,就是一个不可见的进程在后台执行。
Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行,例如我们打开一个音乐播放器来听音乐,在听音乐的同时也想做下其它的事情,比如上网聊Q、或者上网浏览新闻之类的事情。这样的话,我们就需要用到Service服务了。下面我们以一个简单的音乐播放器的实例来说明下Service的生命周期和Service的使用。
 
下面是音乐播放器Demo的程序结构图:
 
 
 
Android Service 的生命周期:
 
Android中Service的生命周期并不是很复杂,只是继承了onCreate(), onStart(), onDestory()三个方法。当我们第一次启动Service服务时,调用onCreate() --> onStart()两个方法,当停止Service服务时,调用onDestory()方法。如果Service已经启动了,第二次再启动同一个服务时,就只是调用 onStart() 这个方法了。
 
Android Service 的使用:
 
[1] 参照上面的程序结构图,我们可以创建一个Android程序,在src目录下创建一个Activity,一个继承自Service类的服务类;同时在资源文件夹res目录下创建一个raw的文件夹存放音频文件,如把music.mp3音乐文件放在该目录下。该程序的主界面如下:
 
 
 
[2] layout目录下的main.xml文件的源码:
 
<?xml version="1.0" encoding="utf-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >   
    <TextView     
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content"    
       android:text="Welcome to Andy's blog!"  
       android:textSize="16sp"/>      
    <TextView     
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content"    
       android:text="音乐播放服务"/>   
    <Button   
       android:id="@+id/startMusic"    
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="开启音乐播放服务"/>   
    <Button   
       android:id="@+id/stopMusic"    
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="停止音乐播放服务"/>   
   <Button   
      android:id="@+id/bindMusic"    
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:text="绑定音乐播放服务"/>   
   <Button   
      android:id="@+id/unbindMusic"    
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:text="解除 ——绑定音乐播放服务"/>   
</LinearLayout>  
[3] src目录下MusicService.java源码:
 
package com.andyidea.service;   
import android.app.Service;   
import android.content.Intent;   
import android.media.MediaPlayer;   
import android.os.IBinder;   
import android.util.Log;   
import android.widget.Toast;   
public class MusicService extends Service {   
    //为日志工具设置标签   
    private static String TAG = "MusicService";   
    //定义音乐播放器变量   
    private MediaPlayer mPlayer;   
       
    //该服务不存在需要被创建时被调用,不管startService()还是bindService()都会启动时调用该方法   
    @Override   
    public void onCreate() {   
        Toast.makeText(this, "MusicSevice onCreate()"  
                , Toast.LENGTH_SHORT).show();   
        Log.e(TAG, "MusicSerice onCreate()");   
           
        mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.music);   
        //设置可以重复播放   
        mPlayer.setLooping(true);   
        super.onCreate();   
    }   
       
    @Override   
    public void onStart(Intent intent, int startId) {   
        Toast.makeText(this, "MusicSevice onStart()"  
                , Toast.LENGTH_SHORT).show();   
        Log.e(TAG, "MusicSerice onStart()");   
           
        mPlayer.start();   
           
        super.onStart(intent, startId);   
    }   
    @Override   
    public void onDestroy() {   
        Toast.makeText(this, "MusicSevice onDestroy()"  
                , Toast.LENGTH_SHORT).show();   
        Log.e(TAG, "MusicSerice onDestroy()");   
           
        mPlayer.stop();   
           
        super.onDestroy();   
    }   
    //其他对象通过bindService 方法通知该Service时该方法被调用   
    @Override   
    public IBinder onBind(Intent intent) {   
        Toast.makeText(this, "MusicSevice onBind()"  
                , Toast.LENGTH_SHORT).show();   
        Log.e(TAG, "MusicSerice onBind()");   
           
        mPlayer.start();   
           
        return null;   
    }   
    //其它对象通过unbindService方法通知该Service时该方法被调用   
    @Override   
    public boolean onUnbind(Intent intent) {   
        Toast.makeText(this, "MusicSevice onUnbind()"  
                , Toast.LENGTH_SHORT).show();   
        Log.e(TAG, "MusicSerice onUnbind()");   
           
        mPlayer.stop();   
           
        return super.onUnbind(intent);   
    }   
       
}  

(编辑:南通站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读