Android中Service服務(wù)詳解(一)
本文詳細(xì)分析了Android中Service服務(wù)。分享給大家供大家參考,具體如下:
一、Service簡介
Service是Android中實(shí)現(xiàn)程序后臺(tái)運(yùn)行的解決方案,適用于去執(zhí)行那些不需要和用戶交互而且還要求長期運(yùn)行的任務(wù)。Service是android 系統(tǒng)中的四大組件之一(Activity、Service、BroadcastReceiver、ContentProvider),它跟Activity的級別差不多,但不能自己運(yùn)行只能后臺(tái)運(yùn)行,并且可以和其他組件進(jìn)行交互。
Service并不是運(yùn)行在一個(gè)獨(dú)立的進(jìn)程當(dāng)中的,而是依賴于創(chuàng)建服務(wù)時(shí)所在的應(yīng)用程序進(jìn)程。當(dāng)某個(gè)應(yīng)用程序進(jìn)程被殺掉時(shí),所有依賴于該進(jìn)程的服務(wù)也會(huì)停止運(yùn)行。
二、Service初實(shí)踐
創(chuàng)建一個(gè)Android項(xiàng)目TestService。
1、新建一個(gè)服務(wù)
package com.example.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
/**
* 服務(wù)第一次創(chuàng)建的時(shí)候調(diào)用
*/
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "服務(wù)的onCreate方法被調(diào)用", Toast.LENGTH_SHORT).show();
}
/**
* 服務(wù)每一次啟動(dòng)的時(shí)候調(diào)用
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "服務(wù)的onStartCommand方法被調(diào)用", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Toast.makeText(this, "服務(wù)的onDestroy方法被調(diào)用", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
在創(chuàng)建一個(gè)服務(wù)時(shí),繼承Service類,重寫了onCreate方法,onStartCommand方法以及onDestroy方法。
2、修改AndroidManifest.xml
當(dāng)新建完一個(gè)服務(wù)后,需要在AndroidManifest.xml中進(jìn)行注冊,如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testservice.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注冊服務(wù) -->
<service android:name=".MyService"></service>
</application>
</manifest>
3、布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="啟動(dòng)服務(wù)" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服務(wù)" />
</LinearLayout>
4、MainActivity.java文件
package com.example.testservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button startService_Button;
private Button stopService_Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取開啟服務(wù)按鈕
startService_Button = (Button) findViewById(R.id.button1);
//獲取停止服務(wù)按鈕
stopService_Button = (Button) findViewById(R.id.button2);
//調(diào)用點(diǎn)擊事件
startService_Button.setOnClickListener(this);
stopService_Button.setOnClickListener(this);
}
/**
* 點(diǎn)擊事件
*/
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button1:
//"開啟服務(wù)"按鈕
Intent startIntent = new Intent(this,MyService.class);
//開啟服務(wù)
startService(startIntent);
break;
case R.id.button2:
//"停止服務(wù)"按鈕
Intent stopIntent = new Intent(this,MyService.class);
//停止服務(wù)
stopService(stopIntent);
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
三、測試結(jié)果
發(fā)布項(xiàng)目后,如下所示:

當(dāng)點(diǎn)擊"啟動(dòng)服務(wù)"按鈕后,會(huì)依次彈出如下:

并且,此時(shí)你多次點(diǎn)擊"啟動(dòng)服務(wù)"按鈕,只會(huì)彈出上方右圖,而不再彈出上方左圖。因?yàn)閮H僅在服務(wù)創(chuàng)建的時(shí)候會(huì)調(diào)用onCreate方法,但當(dāng)服務(wù)啟動(dòng)的時(shí)候每次都會(huì)調(diào)用onStartCommand方法。
當(dāng)點(diǎn)擊"停止服務(wù)"后,如下:

總結(jié):Android Service服務(wù)的啟動(dòng)流程如下:
調(diào)用Context的startService方法---》onCreate方法---》onStartCommand方法---》服務(wù)運(yùn)行。
Android服務(wù)的停止流程如下:
服務(wù)運(yùn)行---》調(diào)用Context的stopService方法--》onDestroy方法---》服務(wù)停止。
更多關(guān)于Android組件相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- 詳解Android中Service服務(wù)的基礎(chǔ)知識(shí)及編寫方法
- Android中Service服務(wù)詳解(二)
- android開發(fā)教程之開機(jī)啟動(dòng)服務(wù)service示例
- Android Service 服務(wù)不被殺死的妙招
- 解析Android中如何做到Service被關(guān)閉后又自動(dòng)啟動(dòng)的實(shí)現(xiàn)方法
- Android中實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)服務(wù)(service)實(shí)例
- android中soap協(xié)議使用(ksoap調(diào)用webservice)
- 在Android中 獲取正在運(yùn)行的Service 實(shí)例
- Android中使用IntentService創(chuàng)建后臺(tái)服務(wù)實(shí)例
- Android中的Service相關(guān)全面總結(jié)
- Android Service服務(wù)不被停止詳解及實(shí)現(xiàn)
相關(guān)文章
Android源碼導(dǎo)入AndroidStudio或IntelliJ?IDEA的方法
這篇文章主要介紹了Android源碼導(dǎo)入AndroidStudio或IntelliJ?IDEA的方法,用idegen來生成針對AndroidStudio或IntelliJ?IDEA的Android系統(tǒng)源代碼工程配置文件,需要的朋友可以參考下2022-08-08
Android顯式啟動(dòng)與隱式啟動(dòng)Activity的區(qū)別介紹
為什么要寫顯式啟動(dòng)與隱式啟動(dòng)Activity,Android的Acitivity啟動(dòng)大致有兩種方式:顯式啟動(dòng)與隱式啟動(dòng),下面分別介紹2014-09-09
Android實(shí)現(xiàn)Window彈窗效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)Window彈窗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Android編程中File文件常見存儲(chǔ)與讀取操作demo示例
這篇文章主要介紹了Android編程中File文件常見存儲(chǔ)與讀取操作,結(jié)合實(shí)例形式分析了Android針對文件的打開、讀寫及布局等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Android Canvas之drawBitmap方法案例詳解
這篇文章主要介紹了Android Canvas之drawBitmap方法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android中ExpandableListView使用示例詳解
這篇文章主要為大家詳細(xì)介紹了Android中ExpandableListView使用示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android DaggerActivityComponent錯(cuò)誤解決辦法詳解
這篇文章主要介紹了Android DaggerActivityComponent錯(cuò)誤解決的相關(guān)資料,需要的朋友可以參考下2017-05-05
解決video標(biāo)簽在安卓webview下無法自動(dòng)播放問題
這篇文章主要介紹了video標(biāo)簽在安卓webview下無法自動(dòng)播放問題的解決方法 ,需要的朋友可以參考下2014-03-03

