Android開發(fā)入門之Service用法分析
本文實(shí)例講述了Android中Service用法。分享給大家供大家參考,具體如下:
關(guān)于Service的講解網(wǎng)上已經(jīng)很多了,這里是關(guān)于自己通過寫代碼Service的一點(diǎn)體會 還有結(jié)合其他人對Service的一點(diǎn)總結(jié)
Service可以理解為一個隱形的Activity 但它又與Activity有些不同,首先Service是沒界面,用戶看不到 可交互的組件 級別是與Activity是差不多的
Service中定義了一系列和自身聲明周期相關(guān)的方法:
onBind(...)是必須實(shí)現(xiàn)的方法,返回一個綁定接口給Service
onCreate(); 當(dāng)Service第一次被創(chuàng)建時 由系統(tǒng)調(diào)用
onStart(...)當(dāng)通過startService()方法調(diào)用啟動Service時被調(diào)用
onDestroy();當(dāng)Service不再使用,系統(tǒng)調(diào)用該方法....
本次代碼分別有MainActivity,Java,MyService.java main.xml這幾個重要文件 下面通過這幾個文件對Service進(jìn)行理解 見注釋
老規(guī)矩 先開始布局 挺簡單的 就是幾個Button
<?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="Service測試" android:gravity="center_horizontal" /> <Button android:id="@+id/startButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Service" /> <Button android:id="@+id/stopButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Stop Service" /> <Button android:id="@+id/bindButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Bind Service" /> <Button android:id="@+id/unBindButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Unbind Service" /> </LinearLayout>
布局效果圖:
開始服務(wù)文件, MyService繼承Service
public class MyService extends Service { final static String TAG = "MyService"; @Override public IBinder onBind(Intent intent) { Log.i(TAG,"OnBind()......"); showInfo("Onbind()......"); return null; } // Setvice創(chuàng)建時被調(diào)用 @Override public void onCreate() { Log.i(TAG,"onCreate()......"); showInfo("onCreate()......"); super.onCreate(); } //當(dāng)客戶調(diào)用startService()啟動Service時被調(diào)用 @Override public void onStart(Intent intent, int startId) { Log.i(TAG,"onStart()........"); showInfo("onStart()........"); super.onStart(intent, startId); } @Override public void onDestroy() { Log.i(TAG,"onDestroy()........"); showInfo("onDestroy()........"); super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { Log.i(TAG,"onUnbind()........"); showInfo("onUnbind()........"); return super.onUnbind(intent); } public void showInfo(String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } }
上面主要是Service中幾個周期函數(shù) 這個MyService代表一個服務(wù),當(dāng)然在這里面我們在里面加什么實(shí)質(zhì)性的東西,例如可以在
Onstart(...)函數(shù)里創(chuàng)建一個音樂播放器MediaPlayer 當(dāng)服務(wù)被啟動時播放音樂.....
你創(chuàng)建了Service 就跟你創(chuàng)建Activity一樣 必須在Manifest里注冊 下面開始注冊
<!-- 增加的Service --> <service android:name=".MyService"> <intent-filter > <action android:name="com.study.android.action.MY_SERVICE"/> </intent-filter> </service>
服務(wù)就這樣 注冊成功。光注冊成功還沒有完成任務(wù)哦...... 還有啟動服務(wù),停止服務(wù),綁定服務(wù),解除綁定的服務(wù)
package com.study.android; import android.app.Activity; import android.app.Service; import android.content.ComponentName; 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 MainActivity extends Activity { final static String ACTION = "com.study.android.action.MY_SERVICE"; private Button startButton,stopButton; private Button bindButton,unbindButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startButton = (Button)findViewById(R.id.startButton); stopButton = (Button)findViewById(R.id.stopButton); bindButton = (Button)findViewById(R.id.bindButton); unbindButton = (Button)findViewById(R.id.unBindButton); // 開啟服務(wù) startButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 啟動服務(wù) startService(buildIntent()); } }); //startButton按下后 周期函數(shù)執(zhí)行順序 onCreate()----->onStart() /******************************************/ // 停止服務(wù) stopButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { stopService(buildIntent()); } }); //stopButton按下后 周期函數(shù)執(zhí)行順序 onDestroy() /******************************************/ // 綁定服務(wù) bindButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { bindService(buildIntent(),conn,Service.BIND_AUTO_CREATE); /*第三個參數(shù):如何創(chuàng)建service 一般是制定綁定時自動創(chuàng)建*/ } }); // bindButton 被按下后(當(dāng)前服務(wù)已經(jīng)stop掉)周期函數(shù)執(zhí)行順序 onCreate()------->onBind() /******************************************/ // 接觸綁定Service unbindButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { unbindService(conn); } }); //unbindButton按下后 周期函數(shù)執(zhí)行順序onUnbind()------->onDestroy() } // 連接對象 private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i("SERVICE","連接服務(wù)成功!"); showInfo("連接服務(wù)成功!"); } @Override public void onServiceDisconnected(ComponentName name) { Log.i("SERVICE","服務(wù)連接斷開!"); showInfo("服務(wù)連接斷開!"); } }; public Intent buildIntent() { Intent intent = new Intent(); // 設(shè)置Intent屬性 注意:這里action屬性要與Manifest里服務(wù)對應(yīng) intent.setAction(ACTION); return intent; } public void showInfo(String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android Service組件使用技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android開發(fā)Jetpack組件DataBinding用例詳解
這篇文章主要為大家介紹了Android開發(fā)Jetpack組件DataBinding的使案用例詳解說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02Android搭建本地Tomcat服務(wù)器及相關(guān)配置
這篇文章主要介紹了Android搭建本地Tomcat服務(wù)器及相關(guān)配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡單的視頻播放示例
這篇文章主要為大家介紹了Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡單的視頻播放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android通過overScrollBy實(shí)現(xiàn)下拉視差特效
這篇文章主要為大家詳細(xì)介紹了Android通過overScrollBy實(shí)現(xiàn)下拉視差特效,實(shí)現(xiàn)精彩的阻尼效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android組件Glide實(shí)現(xiàn)圖片平滑滾動效果
這篇文章主要介紹了Android組件Glide實(shí)現(xiàn)圖片平滑滾動效果的相關(guān)資料,具有一定的參考價值,需要的朋友可以參考下2016-07-07Flutter實(shí)現(xiàn)簡單的內(nèi)容高亮效果
內(nèi)容高亮并不陌生,特別是在搜索內(nèi)容頁面,可以說四處可見,這篇文章主要為大家介紹了如何使用Flutter實(shí)現(xiàn)簡單的內(nèi)容高亮效果,需要的可以參考下2023-08-08Android實(shí)現(xiàn)省市區(qū)三級聯(lián)動
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)省市區(qū)三級聯(lián)動,Spinner實(shí)現(xiàn)省市區(qū)的三級聯(lián)動,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07