Android中Service服務(wù)詳解(一)
本文詳細(xì)分析了Android中Service服務(wù)。分享給大家供大家參考,具體如下:
一、Service簡介
Service是Android中實現(xiàn)程序后臺運行的解決方案,適用于去執(zhí)行那些不需要和用戶交互而且還要求長期運行的任務(wù)。Service是android 系統(tǒng)中的四大組件之一(Activity、Service、BroadcastReceiver、ContentProvider),它跟Activity的級別差不多,但不能自己運行只能后臺運行,并且可以和其他組件進(jìn)行交互。
Service并不是運行在一個獨立的進(jìn)程當(dāng)中的,而是依賴于創(chuàng)建服務(wù)時所在的應(yīng)用程序進(jìn)程。當(dāng)某個應(yīng)用程序進(jìn)程被殺掉時,所有依賴于該進(jìn)程的服務(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
當(dāng)新建完一個服務(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="啟動服務(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ā)布項目后,如下所示:
當(dāng)點擊"啟動服務(wù)"按鈕后,會依次彈出如下:
并且,此時你多次點擊"啟動服務(wù)"按鈕,只會彈出上方右圖,而不再彈出上方左圖。因為僅僅在服務(wù)創(chuàng)建的時候會調(diào)用onCreate方法,但當(dāng)服務(wù)啟動的時候每次都會調(diào)用onStartCommand方法。
當(dāng)點擊"停止服務(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源碼導(dǎo)入AndroidStudio或IntelliJ?IDEA的方法
這篇文章主要介紹了Android源碼導(dǎo)入AndroidStudio或IntelliJ?IDEA的方法,用idegen來生成針對AndroidStudio或IntelliJ?IDEA的Android系統(tǒng)源代碼工程配置文件,需要的朋友可以參考下2022-08-08Android顯式啟動與隱式啟動Activity的區(qū)別介紹
為什么要寫顯式啟動與隱式啟動Activity,Android的Acitivity啟動大致有兩種方式:顯式啟動與隱式啟動,下面分別介紹2014-09-09Android編程中File文件常見存儲與讀取操作demo示例
這篇文章主要介紹了Android編程中File文件常見存儲與讀取操作,結(jié)合實例形式分析了Android針對文件的打開、讀寫及布局等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09Android Canvas之drawBitmap方法案例詳解
這篇文章主要介紹了Android Canvas之drawBitmap方法案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android中ExpandableListView使用示例詳解
這篇文章主要為大家詳細(xì)介紹了Android中ExpandableListView使用示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android DaggerActivityComponent錯誤解決辦法詳解
這篇文章主要介紹了Android DaggerActivityComponent錯誤解決的相關(guān)資料,需要的朋友可以參考下2017-05-05解決video標(biāo)簽在安卓webview下無法自動播放問題
這篇文章主要介紹了video標(biāo)簽在安卓webview下無法自動播放問題的解決方法 ,需要的朋友可以參考下2014-03-03