Android中Service服務(wù)詳解(一)
本文詳細分析了Android中Service服務(wù)。分享給大家供大家參考,具體如下:
一、Service簡介
Service是Android中實現(xiàn)程序后臺運行的解決方案,適用于去執(zhí)行那些不需要和用戶交互而且還要求長期運行的任務(wù)。Service是android 系統(tǒng)中的四大組件之一(Activity、Service、BroadcastReceiver、ContentProvider),它跟Activity的級別差不多,但不能自己運行只能后臺運行,并且可以和其他組件進行交互。
Service并不是運行在一個獨立的進程當中的,而是依賴于創(chuàng)建服務(wù)時所在的應(yīng)用程序進程。當某個應(yīng)用程序進程被殺掉時,所有依賴于該進程的服務(wù)也會停止運行。
二、Service初實踐
創(chuàng)建一個Android項目TestService。
1、新建一個服務(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)建的時候調(diào)用
*/
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "服務(wù)的onCreate方法被調(diào)用", Toast.LENGTH_SHORT).show();
}
/**
* 服務(wù)每一次啟動的時候調(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)建一個服務(wù)時,繼承Service類,重寫了onCreate方法,onStartCommand方法以及onDestroy方法。
2、修改AndroidManifest.xml
當新建完一個服務(wù)后,需要在AndroidManifest.xml中進行注冊,如下:
<?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="啟動服務(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)用點擊事件
startService_Button.setOnClickListener(this);
stopService_Button.setOnClickListener(this);
}
/**
* 點擊事件
*/
@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ā)布項目后,如下所示:

當點擊"啟動服務(wù)"按鈕后,會依次彈出如下:

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

總結(jié):Android Service服務(wù)的啟動流程如下:
調(diào)用Context的startService方法---》onCreate方法---》onStartCommand方法---》服務(wù)運行。
Android服務(wù)的停止流程如下:
服務(wù)運行---》調(diào)用Context的stopService方法--》onDestroy方法---》服務(wù)停止。
更多關(guān)于Android組件相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- 詳解Android中Service服務(wù)的基礎(chǔ)知識及編寫方法
- Android中Service服務(wù)詳解(二)
- android開發(fā)教程之開機啟動服務(wù)service示例
- Android Service 服務(wù)不被殺死的妙招
- 解析Android中如何做到Service被關(guān)閉后又自動啟動的實現(xiàn)方法
- Android中實現(xiàn)開機自動啟動服務(wù)(service)實例
- android中soap協(xié)議使用(ksoap調(diào)用webservice)
- 在Android中 獲取正在運行的Service 實例
- Android中使用IntentService創(chuàng)建后臺服務(wù)實例
- Android中的Service相關(guān)全面總結(jié)
- Android Service服務(wù)不被停止詳解及實現(xiàn)
相關(guān)文章
Android Studio配合WampServer完成本地Web服務(wù)器訪問的問題
這篇文章主要介紹了Android Studio配合WampServer完成本地Web服務(wù)器訪問,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
Android安裝apk文件并適配Android 7.0詳解
這篇文章主要介紹了Android安裝apk文件并適配Android 7.0詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
android開發(fā)框架afinal使用方法小結(jié)
這篇文章主要為大家詳細總結(jié)了android開發(fā)框架afinal使用方法,注解功能、文件上傳下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android中實現(xiàn)下載URL地址的網(wǎng)絡(luò)資源的實例分享
這篇文章主要介紹了Android中實現(xiàn)下載URL地址的網(wǎng)絡(luò)資源的實例,其中還有一個進行多線程下載的Java代碼示例,非常典型,需要的朋友可以參考下2016-04-04

