生活随笔
收集整理的這篇文章主要介紹了
Android Service使用方法--简单音乐播放实例
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? Service翻譯成中文是服務(wù),熟悉Windows 系統(tǒng)的同學(xué)一定很熟悉了。Android里的Service跟Windows里的Service功能差不多,就是一個(gè)不可見的進(jìn)程在后臺(tái)執(zhí)行。
????? Android中的服務(wù),它與Activity不同,它是不能與用戶交互的,不能自己?jiǎn)?dòng)的,運(yùn)行在后臺(tái)的程序,如果我們退出應(yīng)用時(shí),Service進(jìn)程并沒有結(jié)束,它仍然在后臺(tái)運(yùn)行,例如我們打開一個(gè)音樂播放器來聽音樂,在聽音樂的同時(shí)也想做下其它的事情,比如上網(wǎng)聊Q、或者上網(wǎng)瀏覽新聞之類的事情。這樣的話,我們就需要用到Service服務(wù)了。下面我們以一個(gè)簡(jiǎn)單的音樂播放器的實(shí)例來說明下Service的生命周期和Service的使用。
下面是音樂播放器Demo的程序結(jié)構(gòu)圖:
Android Service 的生命周期:
Android中Service的生命周期并不是很復(fù)雜,只是繼承了onCreate(), onStart(), onDestory()三個(gè)方法。當(dāng)我們第一次啟動(dòng)Service服務(wù)時(shí),調(diào)用onCreate() --> onStart()兩個(gè)方法,當(dāng)停止Service服務(wù)時(shí),調(diào)用onDestory()方法。如果Service已經(jīng)啟動(dòng)了,第二次再啟動(dòng)同一個(gè)服務(wù)時(shí),就只是調(diào)用 onStart() 這個(gè)方法了。
Android Service 的使用:
[1] 參照上面的程序結(jié)構(gòu)圖,我們可以創(chuàng)建一個(gè)Android程序,在src目錄下創(chuàng)建一個(gè)Activity,一個(gè)繼承自Service類的服務(wù)類;同時(shí)在資源文件夾res目錄下創(chuàng)建一個(gè)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="音樂播放服務(wù)"/>?? ????<Button?? ???????android:id="@+id/startMusic"??? ???????android:layout_width="wrap_content"?? ???????android:layout_height="wrap_content"?? ???????android:text="開啟音樂播放服務(wù)"/>?? ????<Button?? ???????android:id="@+id/stopMusic"??? ???????android:layout_width="wrap_content"?? ???????android:layout_height="wrap_content"?? ???????android:text="停止音樂播放服務(wù)"/>?? ???<Button?? ??????android:id="@+id/bindMusic"??? ??????android:layout_width="wrap_content"?? ??????android:layout_height="wrap_content"?? ??????android:text="綁定音樂播放服務(wù)"/>?? ???<Button?? ??????android:id="@+id/unbindMusic"??? ??????android:layout_width="wrap_content"?? ??????android:layout_height="wrap_content"?? ??????android:text="解除?——綁定音樂播放服務(wù)"/>?? </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配置文件中添加對(duì)Service的注冊(cè)。即在application節(jié)點(diǎn)中添加
????? <service android:name=".MusicService"/> 進(jìn)行注冊(cè)。
[6] 我們來看下程序運(yùn)行后的Log.e中顯示的Service生命周期
[7] 我們?cè)贏ndroid終端設(shè)備中查看下剛才啟動(dòng)的音樂播放服務(wù),看看我們退出程序后,是不是該程序的服務(wù)還在運(yùn)行的呢?按如下步驟:Menu --> Settings --> Applications --> Running services 。在彈出的 Running services 中可以看到有哪些服務(wù)在運(yùn)行。
這樣我們就看到我們退出程序后,是由于該服務(wù)還在后臺(tái)運(yùn)行著,所以我們的音樂還可以繼續(xù)播放著。就這樣,我們就可以一邊享受音樂,一邊可以聊QQ,或者瀏覽新聞等等。
總結(jié)
以上是生活随笔為你收集整理的Android Service使用方法--简单音乐播放实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。