最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Android中的Service

 更新時(shí)間:2017年03月05日 11:30:57   作者:大鵬待日同風(fēng)起  
這篇文章主要介紹了Android中的Service,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

Service簡(jiǎn)介:

Service是被設(shè)計(jì)用來(lái)在后臺(tái)執(zhí)行一些需要長(zhǎng)時(shí)間運(yùn)行的操作。
Android由于允許Service在后臺(tái)運(yùn)行,甚至在結(jié)束Activity后,因此相對(duì)來(lái)說(shuō),Service相比Activity擁有更高的優(yōu)先級(jí)。

創(chuàng)建Service:

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

其中,onCreate()方法是當(dāng)該Service被創(chuàng)建時(shí)執(zhí)行的方法,onBind()是該Service被綁定時(shí)執(zhí)行的方法。

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

當(dāng)創(chuàng)建了一個(gè)新的Service后,還必須在AndroidManifest.xml文件中對(duì)他進(jìn)行配置,需要在application節(jié)點(diǎn)內(nèi)包含一個(gè)Service標(biāo)記。

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

當(dāng)然,如果你想要你自定義的Service僅能被自己編寫的該應(yīng)用程序使用,還可以在標(biāo)簽內(nèi)添加:

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

讓Service執(zhí)行特定的任務(wù):

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

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

啟動(dòng)和停止Service:

顯式啟動(dòng)一個(gè)Service:

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

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

當(dāng)我們從MainActivity調(diào)用運(yùn)行時(shí),可以在Logcat中觀察到輸出: I/ServiceState: is Running
當(dāng)然,我們也可以停止一個(gè)Service,為了讓我們更清晰的觀察到效果,我們可以在ExampleService類中復(fù)寫onDestroy()方法:

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

可以在MainActivity中通過(guò)以下方式停止一個(gè)Service:

顯示停止一個(gè)Service:注意,寫這里時(shí)更換了一個(gè)Service,并將該自定義的Service定位MyService,已經(jīng)不是之前的ExampleService,不過(guò)您認(rèn)可按照自己之前的繼續(xù)編寫,畢竟方法都是一樣的;-)

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

注意Service的調(diào)用不可嵌套,因此無(wú)論Service被調(diào)用了多少次,對(duì)stopService()停止的一次調(diào)用就會(huì)終止它所匹配運(yùn)行中的Service。

由于Service具有較高的優(yōu)先級(jí),通常不會(huì)被運(yùn)行時(shí)終止,因此可以通過(guò)自終止來(lái)避免后臺(tái)運(yùn)行Service耗費(fèi)系統(tǒng)的資源。具體方法為在onStartCommand()方法中加入stopSelf();但是要注意的是這里的stopSelf()并不是直接終止Service,而是當(dāng)Service的所有功能或請(qǐng)求執(zhí)行完后,將Service停止掉,而不是等待系統(tǒng)回收,停止會(huì)調(diào)用onDestroy()銷毀該Service。

將Service綁定到Activity:

當(dāng)一個(gè)Service在一個(gè)Activity中被調(diào)用的時(shí)候,并不會(huì)隨著Activity的銷毀而銷毀,而是仍有可能繼續(xù)在后臺(tái)運(yùn)行著繼續(xù)占用系統(tǒng)的資源,因此如果實(shí)現(xiàn)當(dāng)Activity銷毀時(shí)自動(dòng)停止與其相關(guān)的服務(wù),將會(huì)極大的節(jié)約系統(tǒng)的資源占用,我們可以通過(guò)以下方式實(shí)現(xiàn)Activity與Service的綁定:

XML布局文件:在該布局文件中實(shí)現(xiàn)了四個(gè)按鈕,分別執(zhí)行啟動(dòng)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">
  <!-- 開(kāi)啟Service -->
  <Button
    android:id="@+id/btnStartService"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:text="@string/startService"/>
  <!-- 關(guān)閉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、在下方我們定義了內(nèi)部類MyBinder,這就是為什么我們這里現(xiàn)在能定義binder的原因
    2、這里我們定義binder成員變量的目的是為了在下文的MainActivity中實(shí)現(xiàn)轉(zhuǎn)型*/
  private MyBinder binder = new MyBinder();
  @Override
  public void onCreate() {
    Log.d("ServiceInfo","創(chuàng)建成功");
    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","開(kāi)始執(zhí)行");
    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系統(tǒng)為了安全防護(hù)和整體的穩(wěn)定性,將每一個(gè)應(yīng)用程序隔離在相應(yīng)的獨(dú)立的“沙盒”之中,因此我們自定義的Service實(shí)際上是運(yùn)行在

    用戶空間的,那么我們又有許多服務(wù)需要引用系統(tǒng)的Service,那么一個(gè)在用戶空間一個(gè)系統(tǒng)空間,他們之間如何實(shí)現(xiàn)通信呢,這就需要Binder了,

   Binder是Android系統(tǒng)中實(shí)現(xiàn)不同進(jìn)程之間通信的一種方式,Binder本身有粘合劑的意思,Binder可以粘合Android系統(tǒng)中的四大組件,因此下方我 們?cè)贛yService類中新建了一個(gè)MyBinder內(nèi)部類,并讓其繼承Binder類,用來(lái)實(shí)現(xiàn)對(duì)MyService的獲取,這樣,你該知道為什我們上文要新建一個(gè)My-Binder的成員變量了吧 ^_^,在下方的MainActivity中你也可以看到相關(guān)實(shí)例的運(yùn)用,

例如

public void 
onServiceConnected(ComponentName
   name, IBinder service),注意這里的service是IBinder類型的,我們下方獲取MyService將會(huì)用到他*/
  class MyBinder extends Binder{
    MyService getService(){
      Log.d("ServiceInfo","成功得到當(dāng)前服務(wù)實(shí)例");
      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該變量用來(lái)標(biāo)識(shí)當(dāng)前的Activity與Service是否正在綁定,因?yàn)槿绻贿M(jìn)行標(biāo)識(shí),如果Activity沒(méi)有
   與Service進(jìn)行綁定,而執(zhí)行解除綁定的操作,會(huì)照成錯(cuò)誤或拋出異常,因?yàn)楫?dāng)接觸綁定時(shí)Android不允許綁定為null */
  /*注意,這里我們新建了一個(gè)connection并重寫了相關(guān)方法,為什么我們要新建這個(gè)連接,那是因?yàn)樵谙路降慕壎ê徒饨?
   方法即bind和unbind需要一個(gè)connection。我們復(fù)寫相關(guān)方法一是為了方便觀察,另一個(gè)是為了在連接成功或失去關(guān)閉
   連接時(shí),執(zhí)行相關(guān)的自定義的任務(wù)或操作*/
  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","關(guān)閉連接");
       //當(dāng)連接指向?qū)嵗秊閚ull沒(méi)有指引的連接的實(shí)例時(shí),好被虛擬機(jī)回收,降低占用的資源
      myService = null;
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //初始化數(shù)據(jù)
    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:
        //開(kāi)啟Service
        startService(serviceIntent);
        break;
      case R.id.btnStopService:
        //關(guān)閉Service
        stopService(serviceIntent);
        break;
      case R.id.btnBindService:
        //綁定Service
        isBond = bindService(serviceIntent,connection,Context.BIND_AUTO_CREATE);
        break;
      case R.id.btnUnbindService:
        //解綁Service,當(dāng)連接為null是解綁會(huì)報(bào)錯(cuò)
        if(isBond){
          unbindService(connection);
          //如果解綁成功,則修改連接標(biāo)識(shí)為假
          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>

 關(guān)于以后:

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

2、有時(shí)間會(huì)編寫一個(gè)簡(jiǎn)單的后臺(tái)播放音樂(lè)的實(shí)例提供給大家參考一下

以上所述是小編給大家介紹的詳解Android中的Service,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

  • Android實(shí)現(xiàn)在TextView文字過(guò)長(zhǎng)時(shí)省略部分或滾動(dòng)顯示的方法

    Android實(shí)現(xiàn)在TextView文字過(guò)長(zhǎng)時(shí)省略部分或滾動(dòng)顯示的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)在TextView文字過(guò)長(zhǎng)時(shí)省略部分或滾動(dòng)顯示的方法,結(jié)合實(shí)例形式分析了Android中TextView控件文字顯示及滾動(dòng)效果相關(guān)操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android Studio 下自動(dòng)注釋(自定義作者,類作用等)圖文詳解

    Android Studio 下自動(dòng)注釋(自定義作者,類作用等)圖文詳解

    android studio 下自動(dòng)注釋功能居然被隱藏了,很多功能都不見(jiàn)了,下面小編通過(guò)本文給大家分享Android Studio 下自動(dòng)注釋(自定義作者,類作用等)圖文詳解,需要的朋友參考下吧
    2017-11-11
  • Android EventBus(普通事件/粘性事件)詳解

    Android EventBus(普通事件/粘性事件)詳解

    這篇文章主要為大家詳細(xì)介紹了Android EventBus 普通事件/粘性事件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Jetpack?Compose入門基礎(chǔ)全面精講

    Jetpack?Compose入門基礎(chǔ)全面精講

    開(kāi)始布局部分。這部分我個(gè)人感覺(jué)沒(méi)有必要每個(gè)組件、屬性都詳細(xì)說(shuō)到,否則篇幅會(huì)很長(zhǎng)。建立起Compose中的組件與?Android?Views的一個(gè)對(duì)應(yīng)關(guān)系就夠了。具體還是需要在實(shí)際的使用中去熟悉
    2022-10-10
  • android生命周期深入分析(二)

    android生命周期深入分析(二)

    Android 程序的生命周期是由系統(tǒng)控制而非程序自身直接控制。這和我們編寫桌面應(yīng)用程序時(shí)的思維有一些不同,本文將詳細(xì)介紹,需要了解的朋友可以參考下
    2012-12-12
  • Android實(shí)現(xiàn)顯示和隱藏密碼功能的示例代碼

    Android實(shí)現(xiàn)顯示和隱藏密碼功能的示例代碼

    在前端中我們知道用javascript就可以可以很容易實(shí)現(xiàn)密碼的顯示與隱藏,本文將大家詳細(xì)介紹Android是如何實(shí)現(xiàn)顯示和隱藏密碼功能的,需要的可以參考一下
    2022-06-06
  • Android高德地圖poi檢索仿微信發(fā)送位置實(shí)例代碼

    Android高德地圖poi檢索仿微信發(fā)送位置實(shí)例代碼

    本篇文章主要介紹了Android高德地圖poi檢索仿微信發(fā)送位置實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-04-04
  • Android中不同狀態(tài)頁(yè)面管理優(yōu)化技巧詳解

    Android中不同狀態(tài)頁(yè)面管理優(yōu)化技巧詳解

    在Android中,不管是activity或者fragment,在加載視圖的時(shí)候都有可能會(huì)出現(xiàn)多種不同的狀態(tài)頁(yè)面View,所以本文就來(lái)聊聊Android中不同狀態(tài)頁(yè)面管理優(yōu)化吧
    2024-04-04
  • Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法

    Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法

    這篇文章主要介紹了Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法,如何為L(zhǎng)istView設(shè)置靜態(tài)數(shù)據(jù),感興趣的小伙伴們可以參考一下
    2015-12-12
  • 詳解Activity之singletast啟動(dòng)模式及如何使用intent傳值

    詳解Activity之singletast啟動(dòng)模式及如何使用intent傳值

    在一個(gè)新棧中創(chuàng)建該Activity實(shí)例,并讓多個(gè)應(yīng)用共享改棧中的該Activity實(shí)例。一旦改模式的Activity的實(shí)例存在于某個(gè)棧中,任何應(yīng)用再激活改Activity時(shí)都會(huì)重用該棧中的實(shí)例,其效果相當(dāng)于多個(gè)應(yīng)用程序共享一個(gè)應(yīng)用,不管誰(shuí)激活該Activity都會(huì)進(jìn)入同一個(gè)應(yīng)用中
    2015-11-11

最新評(píng)論

石阡县| 灌南县| 石台县| 叶城县| 济宁市| 拉孜县| 定襄县| 辽中县| 公主岭市| 娱乐| 即墨市| 鄂州市| 庆元县| 兴国县| 舟山市| 邵武市| 定远县| 环江| 六盘水市| 麻江县| 苏尼特左旗| 图片| 监利县| 富裕县| 阿克陶县| 凯里市| 长丰县| 攀枝花市| 安义县| 陈巴尔虎旗| 阿鲁科尔沁旗| 含山县| 乌兰浩特市| 贵德县| 辉南县| 石棉县| 汾阳市| 乐至县| 越西县| 垣曲县| 克东县|