日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

浅谈Android四大组件之Service

發布時間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅谈Android四大组件之Service 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一:Service簡介

Android開發中,當需要創建在后臺運行的程序的時候,就要使用到Service。

1:Service(服務)是一個沒有用戶界面的在后臺運行執行耗時操作的應用組件。其他應用組件能夠啟動Service,并且當用戶切換到另外的應用場景,Service將持續在后臺運行。另外,一個組件能夠綁定到一個service與之交互(IPC機制),例如,一個service可能會處理網絡操作,播放音樂,操作文件I/O或者與內容提供者(content provider)交互,所有這些活動都是在后臺進行。

?

2:Service有兩種狀態,“啟動的”和“綁定:

通過startService()啟動的服務處于“啟動的”狀態,一旦啟動,service就在后臺運行,即使啟動它的應用組件已經被銷毀了。通常started狀態的service執行單任務并且不返回任何結果給啟動者。比如當下載或上傳一個文件,當這項操作完成時,service應該停止它本身。

?

還有一種“綁定”狀態的service,通過調用bindService()來啟動,一個綁定的service提供一個允許組件與service交互的接口,可以發送請求、獲取返回結果,還可以通過夸進程通信來交互(IPC)。綁定的service只有當應用組件綁定后才能運行,多個組件可以綁定一個service,當調用unbind()方法時,這個service就會被銷毀了。

?

3:serviceactivity一樣都存在與當前進程的主線程中,所以,一些阻塞UI的操作,比如耗時操作不能放在service里進行,比如另外開啟一個線程來處理諸如網絡請求的耗時操作。如果在service里進行一些耗CPU和耗時操作,可能會引發ANR警告,這時應用會彈出是強制關閉還是等待的對話框。所以,對service的理解就是和activity平級的,只不過是看不見的,在后臺運行的一個組件,這也是為什么和activity同被說為Android的基本組件。

?

二:Service的生命周期

Android Service生命周期與Activity生命周期是相似的,但是也存在一些細節上也存在著重要的不同:

onCreateonStart是不同的

通過從客戶端調用Context.startService(Intent)方法我們可以啟動一個服務。如果這個服務還沒有運行,Android將啟動它并且在onCreate方法之后調用它的onStart方法。如果這個服務已經在運行,那么它的onStart方法將被新的Intent再次調用。所以對于單個運行的Service它的onStart方法被反復調用是完全可能的并且是很正常的。

onResumeonPause以及onStop是不需要的

回調一個服務通常是沒有用戶界面的,所以我們也就不需要onPauseonResume或者onStop方法了。無論何時一個運行中的Service它總是在后臺運行。

onBind

如果一個客戶端需要持久的連接到一個服務,那么他可以調用Context.bindService方法。如果這個服務沒有運行方法將通過調用onCreate方法去創建這個服務但并不調用onStart方法來啟動它。相反,onBind方法將被客戶端的Intent調用,并且它返回一個IBind對象以便客戶端稍后可以調用這個服務。同一服務被客戶端同時啟動和綁定是很正常的。

onDestroy

Activity一樣,當一個服務被結束是onDestroy方法將會被調用。當沒有客戶端啟動或綁定到一個服務時Android將終結這個服務。與很多Activity時的情況一樣,當內存很低的時候Android也可能會終結一個服務。如果這種情況發生,Android也可能在內存夠用的時候嘗試啟動被終止的服務,所以你的服務必須為重啟持久保存信息,并且最好在onStart方法內來做。

?

三.Service的啟動方式

Service的啟動方式有如下2種

CstartService()和bindService(),這兩種方式對Service生命周期的影響是不同的

startservice是由其他組件調用startService()方法啟動的,這導致服務的onStartCommand()方法被調用,當服務是started狀態時,其生命周期與啟動它的組件無關,并且可以在后臺無限期運行,即使啟動服務的組件已經被銷毀。因此,服務需要在完成任務后調用stopself()方法停止,或者有其他組件調用stopService()方法停止。

使用bindService()方法啟用服務,調用者與服務綁定在了一起,調用者一旦退出,服務也就終止,大有‘不求同生,但求同死’的特點

Android應用中每個Service都必須要在AndroidMainifest.Xml配置文件中聲明。

?四:2種啟動方式的具體例子

?

startService()方式的生命周期:?
啟動時,startService –> onCreate() –> onStart()

停止時,stopService –> onDestroy()

?

如果調用者直接退出而沒有停止Service,則Service 會一直在后臺運行 Context.startService()方法啟動服務,在服務未被創建時,系統會先調用服務的onCreate()方法,接著調用onStart()方法。如果調用startService()方法前服務已經被創建,多次調用startService()方法并不會導致多次創建服務,但會導致多次調用onStart()方法。采用startService()方法啟動的服務,只能調用Context.stopService()方法結束服務,服務結束時會調用onDestroy()方法。

?

startService()方式啟動 Service的方法:

<?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" > <Button android:id="@+id/startBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="啟動 Service" /> <Button android:id="@+id/stopBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止 Service" /> 
</LinearLayout> 

MainActivity.java

package com.android.service.activity;  import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  public class MainActivity extends Activity  
{  private Button startBtn;  private Button stopBtn;  @Override public void onCreate(Bundle savedInstanceState)  {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  startBtn = (Button) findViewById(R.id.startBtn);  stopBtn = (Button) findViewById(R.id.stopBtn);  //添加監聽器  
        startBtn.setOnClickListener(listener);  stopBtn.setOnClickListener(listener);  }  //啟動監聽器  private OnClickListener listener=new OnClickListener()  {         @Override public void onClick(View v)  {  Intent intent=new Intent(MainActivity.this, ServiceDemo.class);  switch (v.getId())  {  case R.id.startBtn:  startService(intent);  break;  case R.id.stopBtn:  stopService(intent);  break;  default:  break;  }             }  };  
} 

新建ServiceDemo類,ServiceDemo.java如下

package com.android.service.activity;  import android.app.Service;  
import android.content.Intent;  
import android.os.IBinder;  
import android.util.Log;  public class ServiceDemo extends Service  
{  private static final String TAG="Test";  @Override //Service時被調用  public void onCreate()  {  Log.i(TAG, "Service onCreate--->");  super.onCreate();  }  @Override //當調用者使用startService()方法啟動Service時,該方法被調用  public void onStart(Intent intent, int startId)  {  Log.i(TAG, "Service onStart--->");  super.onStart(intent, startId);  }  @Override //當Service不在使用時調用  public void onDestroy()  {  Log.i(TAG, "Service onDestroy--->");  super.onDestroy();  }  @Override //當使用startService()方法啟動Service時,方法體內只需寫return null  public IBinder onBind(Intent intent)  {  return null;  }  
}  

在AndroidManifest.xml文件中添加16~21行的聲明

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.service.activity" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".ServiceDemo" >    <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </service>    </application> 
</manifest> 

效果如下:

當點擊按鈕時,先后執行了Service中onCreate()->onStart()這兩個方法,LogCat顯示如下:

當點擊?按鈕時,Service則執行了onDestroy()方法,LogCat顯示如下:

?

退出后,進入Settings(設置)->Applications(應用)->Running Services(正在運行的服務)看一下我們新啟動了的服務,效果圖如下:

?

?

bindService()方式的生命周期:?
綁定時,bindService -> onCreate() –> onBind()
調用者退出了,即解綁定時,Srevice就會unbindService –>onUnbind() –> onDestory() ? 用Context.bindService()方法啟動服務,在服務未被創建時,系統會先調用服務的onCreate()方法,接著調用onBind()方法。這個時候調用者和服務綁定在一起,調用者退出了,系統就會先調用服務的onUnbind()方法,接著調用onDestroy()方法。如果調用bindService()方法前服務已經被綁定,多次調用bindService()方法并不會導致多次創建服務及綁定(也就是說onCreate()和onBind()方法并不會被多次調用)。如果調用者希望與正在綁定的服務解除綁定,可以調用unbindService()方法,調用該方法也會導致系統調用服務的onUnbind()-->onDestroy()方法。 ?bindService()方式啟動 Service的方法: 綁定Service需要三個參數:bindService(intent, conn, Service.BIND_AUTO_CREATE); 第一個:Intent對象 第二個:ServiceConnection對象,創建該對象要實現它的onServiceConnected()和?onServiceDisconnected()來判斷連接成功或者是斷開連接 第三個:如何創建Service,一般指定綁定的時候自動創建。 例子如下: 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" > <Button   android:text="啟動Service"   android:id="@+id/startBtn1"   android:layout_width="match_parent"   android:layout_height="wrap_content" />    <Button   android:text="停止Service"   android:id="@+id/stopBtn2"   android:layout_width="match_parent"   android:layout_height="wrap_content" />    <Button   android:text="綁定Service"   android:id="@+id/bindBtn3"   android:layout_width="match_parent"   android:layout_height="wrap_content" /> <Button   android:text="解除綁定"   android:id="@+id/unbindBtn4"   android:layout_width="match_parent"   android:layout_height="wrap_content" /> 
</LinearLayout> 

MainActivity.java

package com.android.bindservice.activity;  import android.app.Activity;  
import android.app.Service;  
import android.content.ComponentName;  
import android.content.Intent;  
import android.content.ServiceConnection;  
import android.os.Bundle;  
import android.os.IBinder;  
import android.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  public class MainActivity extends Activity {  // 聲明Button  private Button startBtn,stopBtn,bindBtn,unbindBtn;  @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  // 設置當前布局視圖  
        setContentView(R.layout.main);  // 實例化Button  startBtn = (Button)findViewById(R.id.startBtn1);  stopBtn = (Button)findViewById(R.id.stopBtn2);  bindBtn = (Button)findViewById(R.id.bindBtn3);  unbindBtn = (Button)findViewById(R.id.unbindBtn4);  // 添加監聽器  
        startBtn.setOnClickListener(startListener);  stopBtn.setOnClickListener(stopListener);  bindBtn.setOnClickListener(bindListener);  unbindBtn.setOnClickListener(unBindListener);  }  // 啟動Service監聽器  private OnClickListener startListener = new OnClickListener() {  @Override public void onClick(View v) {  // 創建Intent  Intent intent = new Intent();  // 設置Class屬性  intent.setClass(MainActivity.this, BindService.class);  // 啟動該Service  
            startService(intent);  }  };  // 停止Service監聽器  private OnClickListener stopListener = new OnClickListener() {  @Override public void onClick(View v) {  // 創建Intent  Intent intent = new Intent();  // 設置Class屬性  intent.setClass(MainActivity.this, BindService.class);  // 啟動該Service  
            stopService(intent);  }  };  // 連接對象  private ServiceConnection conn = new ServiceConnection() {  @Override public void onServiceConnected(ComponentName name, IBinder service) {  Log.i("Service", "連接成功!");  }  @Override public void onServiceDisconnected(ComponentName name) {  Log.i("Service", "斷開連接!");  }  };  // 綁定Service監聽器  private OnClickListener bindListener = new OnClickListener() {  @Override public void onClick(View v) {  // 創建Intent  Intent intent = new Intent();  // 設置Class屬性  intent.setClass(MainActivity.this, BindService.class);  // 綁定Service  
            bindService(intent, conn, Service.BIND_AUTO_CREATE);  }  };  // 解除綁定Service監聽器  private OnClickListener unBindListener = new OnClickListener() {  @Override public void onClick(View v) {  // 創建Intent  Intent intent = new Intent();  // 設置Class屬性  intent.setClass(MainActivity.this, BindService.class);  // 解除綁定Service  
            unbindService(conn);  }  };    
} 

BindService.java

package com.android.bindservice.activity;  import android.app.Service;  
import android.content.Intent;  
import android.os.IBinder;  
import android.util.Log;  public class BindService extends Service{  private static final String TAG="Test";  //返回null  public IBinder onBind(Intent intent) {  Log.i(TAG, "Service onBind--->");  return null;  }  // Service創建時調用  public void onCreate() {  Log.i(TAG, "Service onCreate--->");  }  // 當客戶端調用startService()方法啟動Service時,該方法被調用  public void onStart(Intent intent, int startId) {  Log.i(TAG, "Service onStart--->");  }  // 當Service不再使用時調用  public void onDestroy() {  Log.i(TAG, "Service onDestroy--->");  }  // 當解除綁定時調用  public boolean onUnbind(Intent intent) {    Log.i(TAG, "Service onUnbind--->");    return super.onUnbind(intent);    }    
}  

?

在AndroidManifest.xml文件中添加16~21行的聲明

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.bindservice.activity" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".BindService" >    <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </service> </application> 
</manifest> 

效果如下:

當點擊按鈕時,先后執行了Service中onCreate()->onStart()這兩個方法,LogCat顯示如下:

當點擊按鈕,則Service執行了onUnbind()方法,LogCat顯示如下:

?

當點擊按鈕,則Service執行了onUnbind()方法,LogCat顯示如下:

?

?

轉載于:https://www.cnblogs.com/FENGXUUEILIN/p/5663280.html

總結

以上是生活随笔為你收集整理的浅谈Android四大组件之Service的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。