溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解Android中的Service

發布時間:2020-09-07 13:13:37 來源:腳本之家 閱讀:533 作者:大鵬待日同風起 欄目:移動開發

Service簡介:

Service是被設計用來在后臺執行一些需要長時間運行的操作。
Android由于允許Service在后臺運行,甚至在結束Activity后,因此相對來說,Service相比Activity擁有更高的優先級。

創建Service:

要創建一個最基本的Service,需要完成以下工作:1)創建一個Java類,并讓其繼承Service 2)重寫onCreate()和onBind()方法

其中,onCreate()方法是當該Service被創建時執行的方法,onBind()是該Service被綁定時執行的方法。

public class ExampleService extends Service{
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
  @Override
  public void onCreate() {
    super.onCreate();
  }
}

當創建了一個新的Service后,還必須在AndroidManifest.xml文件中對他進行配置,需要在application節點內包含一個Service標記。

<service android:name=".ExampleService" android:enabled="true" android:permission="exam02.chenqian.com.servicedemo"></service>

當然,如果你想要你自定義的Service僅能被自己編寫的該應用程序使用,還可以在標簽內添加:

android:permission="exam02.chenqian.com.servicedemo"

讓Service執行特定的任務:

如果想要Service執行特定的任務,可以復寫Service的onStartCommand()方法,注意在API15之前為onStart()方法,現已不推薦,onStartCommand()方法的執行為該Service onCreate()之后。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  return super.onStartCommand(intent, flags, startId);
}

啟動和停止Service:

顯式啟動一個Service:

// 顯示啟動ExampleService
Intent intent = new Intent(this,ExampleService.class);
// 啟動ExampleService
startService(intent);

為了方便觀察,我們可以在之前創建的自定義的Service類中的onStartCommand()方法中添加Log.i("ServiceState","-------------->is Running");

當我們從MainActivity調用運行時,可以在Logcat中觀察到輸出: I/ServiceState: is Running
當然,我們也可以停止一個Service,為了讓我們更清晰的觀察到效果,我們可以在ExampleService類中復寫onDestroy()方法:

  @Override
  public void onDestroy() {
    Log.i("ServiceState","------------------>Destroy");
    super.onDestroy();
  }

可以在MainActivity中通過以下方式停止一個Service:

顯示停止一個Service:注意,寫這里時更換了一個Service,并將該自定義的Service定位MyService,已經不是之前的ExampleService,不過您認可按照自己之前的繼續編寫,畢竟方法都是一樣的;-)

//顯示關閉Service
Intent serviceIntent = new Intent(MainActivity.this,MyService.class);
//關閉Service
stopService(serviceIntent);

注意Service的調用不可嵌套,因此無論Service被調用了多少次,對stopService()停止的一次調用就會終止它所匹配運行中的Service。

由于Service具有較高的優先級,通常不會被運行時終止,因此可以通過自終止來避免后臺運行Service耗費系統的資源。具體方法為在onStartCommand()方法中加入stopSelf();但是要注意的是這里的stopSelf()并不是直接終止Service,而是當Service的所有功能或請求執行完后,將Service停止掉,而不是等待系統回收,停止會調用onDestroy()銷毀該Service。

將Service綁定到Activity:

當一個Service在一個Activity中被調用的時候,并不會隨著Activity的銷毀而銷毀,而是仍有可能繼續在后臺運行著繼續占用系統的資源,因此如果實現當Activity銷毀時自動停止與其相關的服務,將會極大的節約系統的資源占用,我們可以通過以下方式實現Activity與Service的綁定:

XML布局文件:在該布局文件中實現了四個按鈕,分別執行啟動Service、停止Service、綁定Service和解除綁定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:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="demo.chenqian.com.androidserverdemo.MainActivity">
  <!-- 開啟Service -->
  <Button
    android:id="@+id/btnStartService"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:text="@string/startService"/>
  <!-- 關閉Service -->
  <Button
    android:id="@+id/btnStopService"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:text="@string/stopService"/>
  <!-- 綁定Service -->
  <Button
    android:id="@+id/btnBindService"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:text="@string/bindService"/>
  <!-- 解綁Service -->
  <Button
    android:id="@+id/btnUnbindService"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:text="@string/unbindService"/>
</LinearLayout>

MyService類:

package demo.chenqian.com.androidserverdemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
public class MyService extends Service{
  /* 1、在下方我們定義了內部類MyBinder,這就是為什么我們這里現在能定義binder的原因
    2、這里我們定義binder成員變量的目的是為了在下文的MainActivity中實現轉型*/
  private MyBinder binder = new MyBinder();
  @Override
  public void onCreate() {
    Log.d("ServiceInfo","創建成功");
    super.onCreate();
  }
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    Log.d("ServiceInfo","綁定成功");
    return null;
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("ServiceInfo","開始執行");
    return super.onStartCommand(intent, flags, startId);
  }
  @Override
  public boolean onUnbind(Intent intent) {
    Log.d("ServiceInfo","解綁成功");
    return super.onUnbind(intent);
  }
  @Override
  public void onDestroy() {
    Log.d("ServiceInfo","銷毀成功");
    super.onDestroy();
  }

  /*我們知道Android系統為了安全防護和整體的穩定性,將每一個應用程序隔離在相應的獨立的“沙盒”之中,因此我們自定義的Service實際上是運行在

    用戶空間的,那么我們又有許多服務需要引用系統的Service,那么一個在用戶空間一個系統空間,他們之間如何實現通信呢,這就需要Binder了,

   Binder是Android系統中實現不同進程之間通信的一種方式,Binder本身有粘合劑的意思,Binder可以粘合Android系統中的四大組件,因此下方我 們在MyService類中新建了一個MyBinder內部類,并讓其繼承Binder類,用來實現對MyService的獲取,這樣,你該知道為什我們上文要新建一個My-Binder的成員變量了吧 ^_^,在下方的MainActivity中你也可以看到相關實例的運用,

例如

public void 
onServiceConnected(ComponentName
   name, IBinder service),注意這里的service是IBinder類型的,我們下方獲取MyService將會用到他*/
  class MyBinder extends Binder{
    MyService getService(){
      Log.d("ServiceInfo","成功得到當前服務實例");
      return MyService.this;
    }
  }
}

MainActivity類:

package demo.chenqian.com.androidserverdemo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  private Context mContext;
  private Button btnStartService;
  private Button btnStopService;
  private Button btnBindService;
  private Button btnUnbindService;
  private MyService myService;
  private Intent serviceIntent;
  private boolean isBond;
  /*isBond該變量用來標識當前的Activity與Service是否正在綁定,因為如果不進行標識,如果Activity沒有
   與Service進行綁定,而執行解除綁定的操作,會照成錯誤或拋出異常,因為當接觸綁定時Android不允許綁定為null */
  /*注意,這里我們新建了一個connection并重寫了相關方法,為什么我們要新建這個連接,那是因為在下方的綁定和解綁
   方法即bind和unbind需要一個connection。我們復寫相關方法一是為了方便觀察,另一個是為了在連接成功或失去關閉
   連接時,執行相關的自定義的任務或操作*/
  private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      Log.d("ServiceState","連接成功");
      myService = ((MyService.MyBinder)service).getService();
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
      Log.d("ServiceState","關閉連接");
       //當連接指向實例為null沒有指引的連接的實例時,好被虛擬機回收,降低占用的資源
      myService = null;
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //初始化數據
    mContext = this;
    isBond = false;
    //引入需要用到的組件
    btnStartService = (Button) findViewById(R.id.btnStartService);
    btnStopService = (Button) findViewById(R.id.btnStopService);
    btnBindService = (Button) findViewById(R.id.btnBindService);
    btnUnbindService = (Button) findViewById(R.id.btnUnbindService);
    //為按鈕添加單擊事件
    btnStartService.setOnClickListener(this);
    btnStopService.setOnClickListener(this);
    btnBindService.setOnClickListener(this);
    btnUnbindService.setOnClickListener(this);
  }
  @Override
  protected void onStart() {
    serviceIntent = new Intent(this,MyService.class);
    super.onStart();
  }
  @Override
  public void onClick(View v) {
    switch (v.getId()){
      case R.id.btnStartService:
        //開啟Service
        startService(serviceIntent);
        break;
      case R.id.btnStopService:
        //關閉Service
        stopService(serviceIntent);
        break;
      case R.id.btnBindService:
        //綁定Service
        isBond = bindService(serviceIntent,connection,Context.BIND_AUTO_CREATE);
        break;
      case R.id.btnUnbindService:
        //解綁Service,當連接為null是解綁會報錯
        if(isBond){
          unbindService(connection);
          //如果解綁成功,則修改連接標識為假
          isBond = false;
        }
        break;
    }
  }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="demo.chenqian.com.androidserverdemo">
  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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:permission="demo.chenqian.com.androidserverdemo"></service>
  </application>
</manifest>

 關于以后:

1、感覺Binder那塊還給大家解釋的不太清楚,以后再深入研究下補充完整

2、有時間會編寫一個簡單的后臺播放音樂的實例提供給大家參考一下

以上所述是小編給大家介紹的詳解Android中的Service,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女