Android服務

參考:

一. 什么是服務

服務是一個可以在后臺執行長時間運行操作而不提供用戶界面的應用組件。服務可由其他應用組件啟動,而且即使用戶切換到其他應用,服務仍將在后臺繼續運行。 此外,組件可以綁定到服務,以與之進行交互,甚至是執行進程間通信 (IPC)。 例如,服務可以處理網絡事務、播放音樂,執行文件 I/O 或與內容提供程序交互,而所有這一切均可在后臺進行。

二. 服務的兩種啟動方式

  1. start模式啟動
  • 當應用組件(如 Activity)通過調用 startService()啟動服務時,服務即處于“啟動”狀態。一旦啟動,服務即可在后臺無限期運行,即使啟動服務的組件已被銷毀也不受影響。已啟動的服務通常是執行單一操作,而且不會將結果返回給調用方。例如,它可能通過網絡下載或上傳文件。 操作完成后,服務會自行停止運行。
  1. bind模式啟動
  • 當應用組件通過調用 bindService()綁定到服務時,服務即處于“綁定”狀態(與活動綁定)。綁定服務提供了一個客戶端-服務器接口,允許組件與服務進行交互、發送請求、獲取結果,甚至是利用進程間通信 (IPC) 跨進程執行這些操作。 僅當與另一個應用組件綁定時,綁定服務才會運行。 多個組件可以同時綁定到該服務,但全部取消綁定后,該服務即會被銷毀。

三. Start模式啟動服務

  1. 該模式會回調的方法
/**
     * 該方法是必須實現的抽象方法,用于bind模式啟動,在調用bindService之后會調用,用于將服務和調用組件進行綁定。在start模式的時候返回null即可
     * @param intent 用來綁定該服務的intent
     * @return 返回的就是自定義的用于操作服務的接口
     */
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: ");
        Log.i(TAG, "onBind: intent "+intent);
        Log.i(TAG, "onBind: data "+intent.getStringExtra("data"));
        return mDownloadBinder;
    }
/**
     * 該方法在服務只在第一次啟動/綁定的時候會調用
     */
    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate: ");
        super.onCreate();
    }

    /**
     *
     * 該方法在每次調用startService方法的時候都會調用,
     * 一旦執行此方法,服務即會啟動并可在后臺無限期運行。
     * 服務工作完成后,需要通過調用 stopSelf() 或 stopService() 來停止服務。
     *
     * @param intent 就是用來啟動該服務的Intent,傳遞的數據可以通過該intent拿出來
     * @param flags
     * @param startId
     * @return
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        Log.i(TAG, "onStartCommand: "+intent);
        Log.i(TAG, "onStartCommand: data "+intent.getStringExtra("data"));
        Log.i(TAG, "onStartCommand: flags "+flags);
        Log.i(TAG, "onStartCommand: startId "+startId);
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 該方法在調用stopService/unbindService方法的時候會調用,服務停止運行
     */
    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        super.onDestroy();
    }
  1. 使用該模式啟動服務的步驟
  • 新建sevice類繼承自Service
  • 至少實現onStartCommand回調方法
  • 注冊服務
  • 調用startService(Intent)方法啟動該服務,該服務就會一直在后臺運行,直到調用stopService(Intent)或stopSelf(Intent)方法

四. bind方式啟動服務

  1. 該模式會回調的方法
@Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: ");
        Log.i(TAG, "onBind: intent "+intent);
        Log.i(TAG, "onBind: data "+intent.getStringExtra("data"));
        return mDownloadBinder;
    }

    /**
     * 當與服務連接的所有組件都斷開連接的時候調用(Called when all clients have disconnected
     * from a particular interface published by the service.
     * The default implementation does nothing and returns false.)
     * 
     * @param intent 用來綁定服務的intent
     * @return 默認返回false
     */
    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind: ");
        Log.i(TAG, "onUnbind: intent "+intent.getStringExtra("data"));
        return super.onUnbind(intent);
    }

    /**
     * 當有新的組件綁定到服務的時候會調用該方法
     * @param intent 用來綁定服務的intent
     */
    @Override
    public void onRebind(Intent intent) {
        Log.i(TAG, "onRebind: ");
        super.onRebind(intent);
    }
  1. 使用bind方式啟動服務的步驟
  • 創建Binder類作為操作服務的接口
/**
     * 用于Activity操作Service的類,給與服務綁定的組件一個操作服務的接口
     */
    class DownloadBinder extends Binder{

        public void startDownload(){
            Log.i(TAG, "startDownload: ");
        }

        public void getProgress(){
            Log.i(TAG, "getProgress: ");
        }
    }
  • 實現onBind方法并返回Binder對象
  • Activity實現ServiceConnection類,重寫onServiceConnectedonServiceDisconnected方法
private MyService.DownloadBinder mDownloadBinder;//Binder對象,用來操作服務

    private ServiceConnection mConnection = new ServiceConnection() {
        /**
         * 活動與服務綁定的時候調用
         * @param name
         * @param service
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected: name " + name);
            mDownloadBinder = (MyService.DownloadBinder) service;//將IBinder參數強轉,實例化自定義的Binder對象
            mDownloadBinder.startDownload();//調用Binder的方法操作服務
            mDownloadBinder.getProgress();
        }

        /**
         * 活動與服務解綁的時候調用
         * @param name
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected: ");
        }
    };
  • 調用bindService方法進行綁定
/*參數:Intent,ServiceConnection,標志位(BIND_AUTO_CREATE表示活動與服務綁定之后自動創建服務)*/
bindService(startService, mConnection, BIND_AUTO_CREATE);

五. 前臺服務

  1. 通過調用startForeground方法(借助通知)使服務顯示在通知欄就是前臺服務(如通知欄顯示的QQ這種)
  2. 使用方法(該段代碼寫在Service類的onCreate方法中)
Intent intent = new Intent(this,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("This is title")
                .setContentText("This is text")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1,notification);

六. IntentService

  1. 什么是IntentService?
  • 因為服務是運行在主線程的,所以當需要進行耗時操作的時候,就需要在服務內開啟子線程,然后在運行結束的時候調用stopSelf方法結束該服務。為了避免這一大堆麻煩事,IntentService就誕生了。
  1. IntentService做的事(來自官網)
  1. 使用方法
  • 新建一個類繼承自IntentService
  • 實現無參構造函數并調用父類的有參構造函數(必須)
  • 實現onHandleIntent方法(該方法運行在子線程),在該方法內進行想要的操作
public class MyIntentService extends IntentService {

    private final String TAG = getClass().getSimpleName();

    /**
     * 必須實現的無參構造函數,且必須調用父類有參構造方法
     */
    public MyIntentService(){
        super("MyIntentService");
    }

    /**
     * 處理具體邏輯的抽象方法,該方法運行在子線程
     * @param intent 啟動服務用的intent
     */
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.i(TAG, "onHandleIntent: ");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 服務銷毀時會調用
     */
    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        super.onDestroy();
    }
}
  • 正常調用startService方法啟動服務

七. 源碼

  • MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * start service
     */
    private Button mBtStartService;
    /**
     * stop service
     */
    private Button mBtStopService;
    private final String TAG = getClass().getSimpleName();
    /**
     * bind service
     */
    private Button mBindService;
    /**
     * unbind service
     */
    private Button mUnbindService;

    private MyService.DownloadBinder mDownloadBinder;//Binder對象,用來操作服務

    private ServiceConnection mConnection = new ServiceConnection() {
        /**
         * 活動與服務綁定的時候調用
         * @param name
         * @param service
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected: name " + name);
            mDownloadBinder = (MyService.DownloadBinder) service;//將IBinder參數強轉,實例化自定義的Binder對象
            mDownloadBinder.startDownload();//調用Binder的方法操作服務
            mDownloadBinder.getProgress();
        }

        /**
         * 活動與服務解綁的時候調用
         * @param name
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected: ");
        }
    };
    /**
     * start service
     */
    private Button mBtBindService;
    /**
     * start activity
     */
    private Button mBtStartActivity;
    /**
     * start intentservice
     */
    private Button mStartIntentService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    @Override
    public void onClick(View v) {
        Intent startService = new Intent(MainActivity.this, MyService.class);
        startService.putExtra("data", "data");
        switch (v.getId()) {
            case R.id.bt_bind_service:
                Log.i(TAG, "onClick: " + startService);
                startService(startService);
                break;
            case R.id.bt_stop_service:
                //Log.i(TAG, "onClick: "+startService);
                stopService(startService);
                break;
            case R.id.bind_service:
                /*參數:Intent,ServiceConnection,標志位(BIND_AUTO_CREATE表示活動與服務綁定之后自動創建服務)*/
                bindService(startService, mConnection, BIND_AUTO_CREATE);
                //mDownloadBinder.startDownload();
                break;
            case R.id.unbind_service:
                unbindService(mConnection);
                break;
            case R.id.bt_start_activity:
                startActivity(new Intent(MainActivity.this, SecondActivity.class));
                break;
            case R.id.start_intent_service:
                startService(new Intent(MainActivity.this,MyIntentService.class));
                break;
        }
    }

    private void initView() {
        mBtStartService = (Button) findViewById(R.id.bt_bind_service);
        mBtStartService.setOnClickListener(this);
        mBtStopService = (Button) findViewById(R.id.bt_stop_service);
        mBtStopService.setOnClickListener(this);
        mBindService = (Button) findViewById(R.id.bind_service);
        mBindService.setOnClickListener(this);
        mUnbindService = (Button) findViewById(R.id.unbind_service);
        mUnbindService.setOnClickListener(this);
        mBtBindService = (Button) findViewById(R.id.bt_bind_service);
        mBtBindService.setOnClickListener(this);
        mBtStartActivity = (Button) findViewById(R.id.bt_start_activity);
        mBtStartActivity.setOnClickListener(this);
        mStartIntentService = (Button) findViewById(R.id.start_intent_service);
        mStartIntentService.setOnClickListener(this);
    }
}
  • MyService
public class MyService extends Service {

    private final String TAG = getClass().getSimpleName();

    private DownloadBinder mDownloadBinder = new DownloadBinder();

    /**
     * 用于Activity操作Service的類,給與服務綁定的組件一個操作服務的接口
     */
    class DownloadBinder extends Binder{

        public void startDownload(){
            Log.i(TAG, "startDownload: ");
        }

        public void getProgress(){
            Log.i(TAG, "getProgress: ");
        }
    }

    public MyService() {
    }

    /**
     * 該方法是必須實現的抽象方法,用于bind模式啟動;
     * 在調用bindService之后會調用,用于將服務和調用組件進行綁定。
     * 在start模式的時候返回null即可
     * @param intent 用來綁定該服務的intent
     * @return 返回的就是自定義的用于操作服務的接口
     */
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: ");
        Log.i(TAG, "onBind: intent "+intent);
        Log.i(TAG, "onBind: data "+intent.getStringExtra("data"));
        return mDownloadBinder;
    }

    /**
     * 當與服務連接的所有組件都斷開連接的時候調用(Called when all clients have disconnected
     * from a particular interface published by the service.
     * The default implementation does nothing and returns false.)
     *
     * @param intent 用來綁定服務的intent
     * @return 默認返回false
     */
    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind: ");
        Log.i(TAG, "onUnbind: intent "+intent.getStringExtra("data"));
        return super.onUnbind(intent);
    }

    /**
     * 當有新的組件綁定到服務的時候會調用該方法
     * @param intent 用來綁定服務的intent
     */
    @Override
    public void onRebind(Intent intent) {
        Log.i(TAG, "onRebind: ");
        super.onRebind(intent);
    }

    /**
     * 該方法在服務只在第一次啟動/綁定的時候會調用
     */
    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate: ");
        /*//將該服務變成前臺服務
        Intent intent = new Intent(this,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("This is title")
                .setContentText("This is text")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1,notification);*/
        super.onCreate();
    }

    /**
     *
     * 該方法在每次調用startService方法的時候都會調用,
     * 一旦執行此方法,服務即會啟動并可在后臺無限期運行。
     * 服務工作完成后,需要通過調用 stopSelf() 或 stopService() 來停止服務。
     *
     * @param intent 就是用來啟動該服務的Intent,傳遞的數據可以通過該intent拿出來
     * @param flags
     * @param startId
     * @return
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        Log.i(TAG, "onStartCommand: "+intent);
        Log.i(TAG, "onStartCommand: data "+intent.getStringExtra("data"));
        Log.i(TAG, "onStartCommand: flags "+flags);
        Log.i(TAG, "onStartCommand: startId "+startId);
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 該方法在調用stopService/unbindService方法的時候會調用,服務停止運行
     */
    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        super.onDestroy();
    }
}
  • MyIntentService
public class MyIntentService extends IntentService {

    private final String TAG = getClass().getSimpleName();

    /**
     * 必須實現的無參構造函數,且必須調用父類有參構造方法
     */
    public MyIntentService(){
        super("MyIntentService");
    }

    /**
     * 處理具體邏輯的抽象方法,該方法運行在子線程
     * @param intent 啟動服務用的intent
     */
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.i(TAG, "onHandleIntent: ");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 服務銷毀時會調用
     */
    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy: ");
        super.onDestroy();
    }
}
  • manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="cn.foxnickel.servicedemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">
        </service>
        <service android:name=".MyIntentService"/>

        <activity android:name=".SecondActivity">
        </activity>
    </application>
</manifest>

七. 注意

  1. 服務并不是運行在獨立的進程中的,而是依賴于創建服務的應用程序進程,當創建該服務的進程被殺掉時,服務也會停止。
  2. 服務的所有代碼都運行在主線程中,并不會自己開啟線程。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,546評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,570評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,505評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,017評論 1 313
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,786評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,219評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,287評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,438評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,971評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,796評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,995評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,540評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,230評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,918評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,697評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內容