Android| |Service

1、Service是什麼?

  • Service是一個應用組件
  • Service沒有圖形化界面
  • Service通常通常用來處理耗時比較長的操作,如下載、播放
  • 可以使用Service更新Content Provider,發送Intent以及啓動系統通知等等

對比Activity,是不可見的,在後臺運行的,通常用來處理相對耗時長的操作

對於BroadcastReceiver,如果onReceiver中執行耗時太長,一般選用Service

2、Service不是什麼?

Service不是一個單獨的進程

Servic不是一個線程

3、Service生命週期

我們通過一個簡單的例子來闡述它的週期:

兩個按鈕,點擊綁定和解除

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="chao.pers.testbc2.TestBC2Activity">

   <Button
       android:text="綁定監聽器"
       android:id="@+id/register"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" />
    <Button
        android:text="解除監聽器綁定"
        android:id="@+id/unregister"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

  繼承Service類的FirstService

public class FirstService extends Service{
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("Service onBlind");
        return null;
    }

    //當創建一個Service對象之後,會調用這個方法
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Service onCreate");
    }

    /**
     *
     * @param intent 可以通過intent來向Service傳遞數據
     * @param flags
     * @param startId
     * @return
     */
    @Override
    //進入到這個方法當中,通常會啓動一些線程來操作intent任務
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("flags-->"+flags);
        System.out.println("startId-->"+startId);
        System.out.println("Service onStartCommand");
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        System.out.println("Service onDestory");
        super.onDestroy();
    }
}

  當然我們還需要在Manifest.xml文件中註冊該Service

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

佈局很簡單,就是兩個按鈕,分別綁定事件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="chao.pers.testservice1.TestActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <Button
        android:text="StartService"
        android:id="@+id/startService"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="StopService"
        android:id="@+id/stopService"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

我們直接點擊兩次STARTSERVICE

發現只調用了一次onCreate方法

點擊STOPSERVICE

4、啓動和停止Service的方法

啓動Service:Context.startService();

停止Service:Context.stopService();

Activity是Context的子類

當第一次調用startService方法的時候,會調用Service類中的onCreate方法和onStartCommand方法,當再次調用startService方法的時候,不再會去調用onCreate方法,而是直接調用onStartCommand方法,當調用stopService方法的時候,會調用onDestroy方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章