生活随笔
收集整理的這篇文章主要介紹了
Android Service使用方法--简单音乐播放实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? 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文件的源碼:
[c-sharp]?view plaincopy
<?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源碼:
[c-sharp]?view plaincopy
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;?? ?????? ?????? ????@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();?? ????}?? ?????? ????@Override?? ????public?IBinder?onBind(Intent?intent)?{?? ????????Toast.makeText(this,?"MusicSevice?onBind()"?? ????????????????,?Toast.LENGTH_SHORT).show();?? ????????Log.e(TAG,?"MusicSerice?onBind()");?? ?????????? ????????mPlayer.start();?? ?????????? ????????return?null;?? ????}?? ?????? ????@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);?? ????}?? ?????? }??
[4] src目錄下MusicServiceActivity源碼:
[c-sharp]?view plaincopy
package?com.andyidea.service;?? import?android.app.Activity;?? import?android.content.ComponentName;?? import?android.content.Context;?? import?android.content.Intent;?? import?android.content.ServiceConnection;?? import?android.os.Bundle;?? import?android.os.IBinder;?? import?android.util.Log;?? import?android.view.View;?? import?android.view.View.OnClickListener;?? import?android.widget.Button;?? import?android.widget.Toast;?? public?class?MusicServiceActivity?extends?Activity?{?? ?????? ?????? ????private?static?String?TAG?=?"MusicService";?? ?????? ?????? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.main);?? ?????????? ?????????? ????????Toast.makeText(this,?"MusicServiceActivity",?? ????????????????Toast.LENGTH_SHORT).show();?? ????????Log.e(TAG,?"MusicServiceActivity");?? ?????????? ????????initlizeViews();?? ????}?? ?????? ????private?void?initlizeViews(){?? ????????Button?btnStart?=?(Button)findViewById(R.id.startMusic);?? ????????Button?btnStop?=?(Button)findViewById(R.id.stopMusic);?? ????????Button?btnBind?=?(Button)findViewById(R.id.bindMusic);?? ????????Button?btnUnbind?=?(Button)findViewById(R.id.unbindMusic);?? ?????????? ?????????? ????????OnClickListener?ocl?=?new?OnClickListener()?{?? ?????????????? ????????????@Override?? ????????????public?void?onClick(View?v)?{?? ?????????????????? ????????????????Intent?intent?=?new?Intent(MusicServiceActivity.this,MusicService.class);?? ????????????????switch(v.getId()){?? ????????????????case?R.id.startMusic:?? ?????????????????????? ????????????????????startService(intent);?? ????????????????????break;?? ????????????????case?R.id.stopMusic:?? ?????????????????????? ????????????????????stopService(intent);?? ????????????????????break;?? ????????????????case?R.id.bindMusic:?? ?????????????????????? ????????????????????bindService(intent,?conn,?Context.BIND_AUTO_CREATE);?? ????????????????????break;?? ????????????????case?R.id.unbindMusic:?? ?????????????????????? ????????????????????unbindService(conn);?? ????????????????????break;?? ????????????????}?? ????????????}?? ????????};?? ?????????? ??????????? ????????btnStart.setOnClickListener(ocl);?? ????????btnStop.setOnClickListener(ocl);?? ????????btnBind.setOnClickListener(ocl);?? ????????btnUnbind.setOnClickListener(ocl);?? ????}?? ?????? ?????? ????final?ServiceConnection?conn?=?new?ServiceConnection()?{?? ?????????? ????????@Override?? ????????public?void?onServiceDisconnected(ComponentName?name)?{?? ????????????Toast.makeText(MusicServiceActivity.this,?"MusicServiceActivity?onSeviceDisconnected"?? ????????????????????,?Toast.LENGTH_SHORT).show();?? ????????????Log.e(TAG,?"MusicServiceActivity?onSeviceDisconnected");?? ????????}?? ?????????? ????????@Override?? ????????public?void?onServiceConnected(ComponentName?name,?IBinder?service)?{?? ????????????Toast.makeText(MusicServiceActivity.this,?"MusicServiceActivity?onServiceConnected"?? ????????????????????,Toast.LENGTH_SHORT).show();?? ????????????Log.e(TAG,?"MusicServiceActivity?onServiceConnected");?? ????????}?? ????};?? }??
[5] 最后,我們別忘了在AndroidManifest.xml配置文件中添加對Service的注冊。即在application節點中添加
????? <service android:name=".MusicService"/> 進行注冊。
[6] 我們來看下程序運行后的Log.e中顯示的Service生命周期
[7] 我們在Android終端設備中查看下剛才啟動的音樂播放服務,看看我們退出程序后,是不是該程序的服務還在運行的呢?按如下步驟:Menu --> Settings --> Applications --> Running services 。在彈出的 Running services 中可以看到有哪些服務在運行。
這樣我們就看到我們退出程序后,是由于該服務還在后臺運行著,所以我們的音樂還可以繼續播放著。就這樣,我們就可以一邊享受音樂,一邊可以聊QQ,或者瀏覽新聞等等。
總結
以上是生活随笔為你收集整理的Android Service使用方法--简单音乐播放实例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。