Android入門:Service入門介紹

來源:互聯網
上載者:User


一、Service介紹

Service類似於Windows中的服務,沒有介面,只是在後台運行;而服務不能自己運行,而是需要調用Context.startService(Intent intent);或Context.bindService(Intent intent)開啟服務;

服務分為兩種:

(1)訪問者與服務無關,則關閉訪問者之後,服務能夠繼續運行,使用startService();

(2)訪問者與服務有關(比如訪問者需要調用服務提供的方法),則關閉訪問者後,服務也要關閉,使用bindService();


服務分為:

(1)本地服務:訪問者和服務在一個應用中;

(2)遠程服務:訪問者和服務在不同應用者;

註:如果我們想讓服務開機自動運行,則可以建立一個廣播接收者,並在onReceive中調用開啟服務代碼;

我們這裡只介紹“訪問者與服務無關”的情況,以後再講另一種情況;

二、Service核心代碼

(1)建立繼承Service的類

(2)在AndroidManifest.xml中配置<service>

(3)覆寫Service中onCreate();方法

開始服務:


Intent service = new Intent(Context,XxxService.class);

Context.startService(service);

然後會調用onCreate()方法;

停止服務:


Intent service = new Intent(Context,XxxService.class);

Context.stopService(service);

然後會調用onDestroy()方法;

三、Service生命週期

1.通過Context.startService()啟動服務

onCreate()-->onStart()-->onDestroy()

onCreate()在建立服務時調用,如果調用多次startService(),onCreate()方法仍然只被調用一次;

onStart()在開始startService()調用時被調用,多次startService(),onStart()方法會被調用多次;

onDestroy()在終止服務時調用;

2.通過Context.bindService()啟動服務

onCreate()-->onBind()-->onUnbind()-->onDestroy()

onBind()在綁定服務時調用,如果調用多次bindService(),則onBind()方法只被調用一次;

onUnbind()在解除綁定(unBindService)時調用;

四、MediaPlayer核心代碼

Mediaplayer player = MediaPlayer.create(getApplicationContext(), R.raw.xiazdong);//建立MediaPlayer對象

player.setLooping(true);//迴圈播放

player.start();  //開始播放

player.stop();//停止播放

player.release();//釋放資源

四、將MP3播放應用於Service


此處只是實現點擊Activity的播放音樂,則開始播放音樂,播放音樂的代碼放到Service中,因此就算關閉應用,音樂也不會停止;

效果如下:


MainActivity中點擊按鈕後執行下面代碼:


private OnClickListener listener = new OnClickListener(){@Overridepublic void onClick(View v) {if(v==button){Intent service = new Intent(MainActivity.this,MediaPlayerService.class);MainActivity.this.startService(service);//開啟服務}if(v==button2){Intent name = new Intent(MainActivity.this,MediaPlayerService.class);MainActivity.this.stopService(name);//停止服務}}};

MediaPlayerService.java

package com.xiazdong.mediaplayer;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;public class MediaPlayerService extends Service {private MediaPlayer player;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();player = MediaPlayer.create(getApplicationContext(), R.raw.xiazdong);player.setLooping(true);try {//因為MediaPlayer的create已經調用了prepare方法,因此此處直接start方法即可player.start();  } catch (Exception e) {e.printStackTrace();}  }@Overridepublic void onDestroy() {//停止服務super.onDestroy();if(player!=null){player.stop();player.release();player = null;}}}

在AndroidManifest.xml中添加:

 <service android:name=".MediaPlayerService"/>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.