Android Service(不和用戶交互應(yīng)用組件)案例分析
Service和其他的應(yīng)用組件一樣,運(yùn)行在進(jìn)程的主線程中。這就是說如果service需要很多耗時或者阻塞的操作,需要在其子線程中實(shí)現(xiàn)。
service的兩種模式
本地服務(wù) Local Service 用于應(yīng)用程序內(nèi)部。
它可以啟動并運(yùn)行,直至有人停止了它或它自己停止。在這種方式下,它以調(diào)用Context.startService()啟動,而以調(diào)用Context.stopService()結(jié)束。它可以調(diào)用Service.stopSelf() 或 Service.stopSelfResult()來自己停止。不論調(diào)用了多少次startService()方法,你只需要調(diào)用一次stopService()來停止服務(wù)。
用于實(shí)現(xiàn)應(yīng)用程序自己的一些耗時任務(wù),比如查詢升級信息,并不占用應(yīng)用程序比如Activity所屬線程,而是單開線程后臺執(zhí)行,這樣用戶體驗(yàn)比較好。
遠(yuǎn)程服務(wù) Remote Service 用于android系統(tǒng)內(nèi)部的應(yīng)用程序之間。
它可以通過自己定義并暴露出來的接口進(jìn)行程序操作。客戶端建立一個到服務(wù)對象的連接,并通過那個連接來調(diào)用服務(wù)。連接以調(diào)用Context.bindService()方法建立,以調(diào)用 Context.unbindService()關(guān)閉。多個客戶端可以綁定至同一個服務(wù)。如果服務(wù)此時還沒有加載,bindService()會先加載它。
可被其他應(yīng)用程序復(fù)用,比如天氣預(yù)報服務(wù),其他應(yīng)用程序不需要再寫這樣的服務(wù),調(diào)用已有的即可。
startService()/bindService()不是完全分離的.
生命周期
Service的生命周期并不像Activity那么復(fù)雜,它只繼承了onCreate(),onStart(),onDestroy()三個方法,當(dāng)我們第一次啟動Service時,先后調(diào)用了onCreate(),onStart()這兩個方法,當(dāng)停止Service時,則執(zhí)行onDestroy()方法,這里需要注意的是,如果Service已經(jīng)啟動了,當(dāng)我們再次啟動Service時,不會在執(zhí)行onCreate()方法,而是直接執(zhí)行onStart()方法。
startService和bindService的區(qū)別
1. 使用startService()方法啟用服務(wù),調(diào)用者與服務(wù)之間沒有關(guān)連,即使調(diào)用者退出了,服務(wù)仍然運(yùn)行。
如果打算采用Context.startService()方法啟動服務(wù),在服務(wù)未被創(chuàng)建時,系統(tǒng)會先調(diào)用服務(wù)的onCreate()方法,接著調(diào)用onStart()方法。
如果調(diào)用startService()方法前服務(wù)已經(jīng)被創(chuàng)建,多次調(diào)用startService()方法并不會導(dǎo)致多次創(chuàng)建服務(wù),但會導(dǎo)致多次調(diào)用onStart()方法。
采用startService()方法啟動的服務(wù),只能調(diào)用Context.stopService()方法結(jié)束服務(wù),服務(wù)結(jié)束時會調(diào)用onDestroy()方法。
2. 使用bindService()方法啟用服務(wù),調(diào)用者與服務(wù)綁定在了一起,調(diào)用者一旦退出,服務(wù)也就終止,大有“不求同時生,必須同時死”的特點(diǎn)。
onBind()只有采用Context.bindService()方法啟動服務(wù)時才會回調(diào)該方法。該方法在調(diào)用者與服務(wù)綁定時被調(diào)用,當(dāng)調(diào)用者與服務(wù)已經(jīng)綁定,多次調(diào)用Context.bindService()方法并不會導(dǎo)致該方法被多次調(diào)用。
采用Context.bindService()方法啟動服務(wù)時只能調(diào)用onUnbind()方法解除調(diào)用者與服務(wù)解除,服務(wù)結(jié)束時會調(diào)用onDestroy()方法。
利用service進(jìn)行音樂播放的例子
先看最終效果

代碼清單:
布局文件
<?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 my 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>
MusicService
package com.example.musicservice;
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 {
//為日志工具設(shè)置標(biāo)簽
private static String TAG = "MusicService";
//定義音樂播放器變量
private MediaPlayer mPlayer;
//該服務(wù)不存在需要被創(chuàng)建時被調(diào)用,不管startService()還是bindService()都會啟動時調(diào)用該方法
@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);
//設(shè)置可以重復(fù)播放
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時該方法被調(diào)用
@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時該方法被調(diào)用
@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);
}
}
MainActivy(調(diào)用service)
package com.example.musicservice;
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.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
//為日志工具設(shè)置標(biāo)簽
private static String TAG = "MusicService";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//輸出Toast消息和日志記錄
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);
//定義點(diǎn)擊監(jiān)聽器
OnClickListener ocl = new OnClickListener() {
@Override
public void onClick(View v) {
//顯示指定 intent所指的對象是個 service
Intent intent = new Intent(MainActivity.this,MusicService.class);
switch(v.getId()){
case R.id.startMusic:
//開始服務(wù)
startService(intent);
break;
case R.id.stopMusic:
//停止服務(wù)
stopService(intent);
break;
case R.id.bindMusic:
//綁定服務(wù)
bindService(intent, conn, Context.BIND_AUTO_CREATE);
break;
case R.id.unbindMusic:
//解綁服務(wù)
unbindService(conn);
break;
}
}
};
//綁定點(diǎn)擊監(jiān)聽
btnStart.setOnClickListener(ocl);
btnStop.setOnClickListener(ocl);
btnBind.setOnClickListener(ocl);
btnUnbind.setOnClickListener(ocl);
}
//定義服務(wù)鏈接對象
final ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(MainActivity.this, "MusicServiceActivity onSeviceDisconnected"
, Toast.LENGTH_SHORT).show();
Log.e(TAG, "MusicServiceActivity onSeviceDisconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(MainActivity.this, "MusicServiceActivity onServiceConnected"
,Toast.LENGTH_SHORT).show();
Log.e(TAG, "MusicServiceActivity onServiceConnected");
}
};
}
最終效果是:
當(dāng)按第一個按鈕的時候,使用startService()方法啟用服務(wù),即使退出activity,服務(wù)還在,音樂會繼續(xù)播放。查看程序運(yùn)行狀態(tài):

如果是使用bindService()方法啟用服務(wù),調(diào)用者與服務(wù)綁定在了一起,退出Activy的同時,Service也停掉了。
通過startService(),我們退出程序后,可以釋放activty的資源,由于該服務(wù)還在后臺運(yùn)行著,所以我們的音樂還可以繼續(xù)播放著,。就這樣,我們就可以一邊享受音樂,一邊可以聊QQ,或者瀏覽新聞等等。
- Android中實(shí)現(xiàn)開機(jī)自動啟動服務(wù)(service)實(shí)例
- android開發(fā)教程之開機(jī)啟動服務(wù)service示例
- android教程之service使用方法示例詳解
- Android創(chuàng)建服務(wù)之started service詳細(xì)介紹
- 解析Android中如何做到Service被關(guān)閉后又自動啟動的實(shí)現(xiàn)方法
- 基于Android Service 生命周期的詳細(xì)介紹
- 在Android中 獲取正在運(yùn)行的Service 實(shí)例
- Android中的Service相關(guān)全面總結(jié)
- Android的Service應(yīng)用程序組件基本編寫方法
- Android提高之Service用法實(shí)例解析
相關(guān)文章
Android app開發(fā)中的Fragment入門學(xué)習(xí)教程
這篇文章主要介紹了Android app開發(fā)中的Fragment入門學(xué)習(xí)教程,包括Fragment的創(chuàng)建和XML布局文件中的Fragment定義等,需要的朋友可以參考下2016-02-02Android 自定義SeekBar 實(shí)現(xiàn)分段顯示不同背景顏色的示例代碼
這篇文章主要介紹了Android 自定義SeekBar 實(shí)現(xiàn)分段顯示不同背景顏色,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06android studio 3.6.1導(dǎo)入項(xiàng)目報錯提示無法下載classpath里的內(nèi)容
這篇文章主要介紹了android studio 3.6.1導(dǎo)入項(xiàng)目報錯提示無法下載classpath里的內(nèi)容,本文通過原因分析通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05Android自定義控件實(shí)現(xiàn)方向盤效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)方向盤效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04Flutter實(shí)現(xiàn)彈窗攔截器的示例代碼
彈窗的排隊(duì)執(zhí)行在App中是一個很常見的應(yīng)用場景,這篇文章為大家介紹了兩個Flutter實(shí)現(xiàn)彈窗攔截器的示例代碼,感興趣的小伙伴可以學(xué)習(xí)一下2023-09-09Android中的ViewPager視圖滑動切換類的入門實(shí)例教程
Android中ViewPager通常與Fragments組件共同使用來實(shí)現(xiàn)視圖切換功能,本文就帶大家一起來學(xué)習(xí)Android中的ViewPager視圖滑動切換類的入門實(shí)例教程:2016-06-06Activity實(shí)例詳解之啟動activity并返回結(jié)果
這篇文章主要介紹了Activity實(shí)例詳解之啟動activity并返回結(jié)果 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09詳解Android App卸載后跳轉(zhuǎn)到指定的反饋頁面的方法
這篇文章主要介紹了Android App卸載后跳轉(zhuǎn)到指定的反饋頁面的方法,關(guān)鍵點(diǎn)是相關(guān)線程要判斷在目錄被消除以前作出響應(yīng),需要的朋友可以參考下2016-04-04