欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android使用Service實現(xiàn)簡單音樂播放實例-創(chuàng)新互聯(lián)

      Service翻譯成中文是服務,熟悉Windows 系統(tǒng)的同學一定很熟悉了。Android里的Service跟Windows里的Service功能差不多,就是一個不可見的進程在后臺執(zhí)行。

夏津ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

      Android中的服務,它與Activity不同,它是不能與用戶交互的,不能自己啟動的,運行在后臺的程序,如果我們退出應用時,Service進程并沒有結束,它仍然在后臺運行,例如我們打開一個音樂播放器來聽音樂,在聽音樂的同時也想做下其它的事情,比如上網(wǎng)聊Q、或者上網(wǎng)瀏覽新聞之類的事情。這樣的話,我們就需要用到Service服務了。下面我們以一個簡單的音樂播放器的實例來說明下Service的生命周期和Service的使用。

下面是音樂播放器Demo的程序結構圖:

Android使用Service實現(xiàn)簡單音樂播放實例

Android Service 的生命周期:

Android中Service的生命周期并不是很復雜,只是繼承了onCreate(), onStart(), onDestory()三個方法。當我們第一次啟動Service服務時,調用onCreate() --> onStart()兩個方法,當停止Service服務時,調用onDestory()方法。如果Service已經(jīng)啟動了,第二次再啟動同一個服務時,就只是調用 onStart() 這個方法了。

Android Service 的使用:

[1] 參照上面的程序結構圖,我們可以創(chuàng)建一個Android程序,在src目錄下創(chuàng)建一個Activity,一個繼承自Service類的服務類;同時在資源文件夾res目錄下創(chuàng)建一個raw的文件夾存放音頻文件,如把music.mp3音樂文件放在該目錄下。該程序的主界面如下:

[2] layout目錄下的main.xml文件的源碼:

<?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 Andy's blog!" 
    android:textSize="16sp"/>   
  <TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="音樂播放服務"/> 
  <Button 
    android:id="@+id/startMusic"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="開啟音樂播放服務"/> 
  <Button 
    android:id="@+id/stopMusic"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="停止音樂播放服務"/> 
  <Button 
   android:id="@+id/bindMusic"  
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="綁定音樂播放服務"/> 
  <Button 
   android:id="@+id/unbindMusic"  
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="解除 ——綁定音樂播放服務"/> 
</LinearLayout> 

文章標題:Android使用Service實現(xiàn)簡單音樂播放實例-創(chuàng)新互聯(lián)
標題URL:http://www.chinadenli.net/article38/cchcsp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號品牌網(wǎng)站制作Google網(wǎng)站排名域名注冊服務器托管

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設計公司