欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中Service服務(wù)詳解(二)

 更新時(shí)間:2016年01月15日 11:50:06   作者:殘缺的孤獨(dú)  
這篇文章主要介紹了Android中Service服務(wù),在前面一篇的基礎(chǔ)上進(jìn)一步分析了Android Service的綁定服務(wù)與解綁服務(wù)的相關(guān)使用技巧,需要的朋友可以參考下

本文詳細(xì)分析了Android中Service服務(wù)。分享給大家供大家參考,具體如下:

在前面文章《Android中Service服務(wù)詳解(一)》中,我們介紹了服務(wù)的啟動(dòng)和停止,是調(diào)用Context的startService和stopService方法。還有另外一種啟動(dòng)方式和停止方式,即綁定服務(wù)和解綁服務(wù),這種方式使服務(wù)與啟動(dòng)服務(wù)的活動(dòng)之間的關(guān)系更為緊密,可以在活動(dòng)中告訴服務(wù)去做什么事情。

為了說明這種情況,做如下工作:

1、修改Service服務(wù)類MyService

package com.example.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
  //創(chuàng)建自己的綁定服務(wù)業(yè)務(wù)邏輯
  class MusicBinder extends Binder{
    public void ready(){
      Log.d("MyService", "----ready Method---");
    }
    public void play(){
      Log.d("MyService", "----play Method---");
    }
  }
  private MusicBinder musicBinder = new MusicBinder();
  @Override
  public IBinder onBind(Intent arg0) {
    Toast.makeText(this, "服務(wù)的onBind方法被調(diào)用", Toast.LENGTH_SHORT).show();
    return musicBinder;
  }
  /**
   * 服務(wù)第一次創(chuàng)建的時(shí)候調(diào)用
   */
  @Override
  public void onCreate() {
    super.onCreate();
    Toast.makeText(this, "服務(wù)的onCreate方法被調(diào)用", Toast.LENGTH_SHORT).show();
  }
  /**
   * 服務(wù)每一次啟動(dòng)的時(shí)候調(diào)用
   */
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "服務(wù)的onStartCommand方法被調(diào)用", Toast.LENGTH_SHORT).show();
    return super.onStartCommand(intent, flags, startId);
  }
  @Override
  public void onDestroy() {
    Toast.makeText(this, "服務(wù)的onDestroy方法被調(diào)用", Toast.LENGTH_SHORT).show();
    super.onDestroy();
  }
}

在服務(wù)類中,添加了內(nèi)部類MusicBinder,在該內(nèi)部類中,我們模擬了兩個(gè)方法。同時(shí)在onBind方法中返回我們內(nèi)部類實(shí)例。

2、修改布局文件activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="啟動(dòng)服務(wù)" />
  <Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="停止服務(wù)" />
  <Button
    android:id="@+id/button3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="綁定服務(wù)" />
  <Button
    android:id="@+id/button4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="解綁服務(wù)" />
</LinearLayout>

即添加了兩個(gè)按鈕:“綁定服務(wù)”和“解綁服務(wù)”按鈕。

3、修改MainActivity.java文件

package com.example.testservice;
import com.example.testservice.MyService.MusicBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
  private Button startService_Button;
  private Button stopService_Button;
  private Button bindService_Button;
  private Button unbindService_Button;
  private MyService.MusicBinder musicBinder;
  //創(chuàng)建ServiceConnection,在綁定服務(wù)的時(shí)候會(huì)用到。
  private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceDisconnected(ComponentName service) {
    }
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      //類型轉(zhuǎn)換
      musicBinder = (MyService.MusicBinder) service;
      //指揮服務(wù)需要做的工作
      musicBinder.ready();
      musicBinder.play();
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //獲取開啟服務(wù)按鈕
    startService_Button = (Button) findViewById(R.id.button1);
    //獲取停止服務(wù)按鈕
    stopService_Button = (Button) findViewById(R.id.button2);
    //獲取綁定服務(wù)按鈕
    bindService_Button = (Button) findViewById(R.id.button3);
    //獲取解綁服務(wù)按鈕
    unbindService_Button = (Button) findViewById(R.id.button4);
    //調(diào)用點(diǎn)擊事件
    startService_Button.setOnClickListener(this);
    stopService_Button.setOnClickListener(this);
    bindService_Button.setOnClickListener(this);
    unbindService_Button.setOnClickListener(this);
  }
  /**
   * 點(diǎn)擊事件
   */
  @Override
  public void onClick(View view) {
    switch(view.getId()){
    case R.id.button1:
      //“開啟服務(wù)”按鈕
      Intent startIntent = new Intent(this,MyService.class);
      //開啟服務(wù)
      startService(startIntent);
      break;
    case R.id.button2:
      //“停止服務(wù)”按鈕
      Intent stopIntent = new Intent(this,MyService.class);
      //停止服務(wù)
      stopService(stopIntent);
      break;
    case R.id.button3:
      //“綁定服務(wù)”按鈕
      Intent bindIntent = new Intent(this,MyService.class);
      bindService(bindIntent, connection, BIND_AUTO_CREATE);
      break;
    case R.id.button4:
      //“解綁服務(wù)”按鈕
      Intent unbindIntent = new Intent(this,MyService.class);
      unbindService(connection);
      break;
    default:
      break;
    }
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
}

在該類中,創(chuàng)建了ServiceConnection的匿名類,即當(dāng)活動(dòng)和服務(wù)建立連接后,需要做的工作,在onServiceConnected方法中我們進(jìn)行了綁定服務(wù)的類型轉(zhuǎn)換,然后調(diào)用相應(yīng)的業(yè)務(wù)邏輯方法。

活動(dòng)和服務(wù)的綁定石在onClick方法中實(shí)現(xiàn)的:使用bindService方法進(jìn)行綁定,使MainActivity活動(dòng)和MyService服務(wù)綁定在一起,其中第三個(gè)參數(shù)是個(gè)標(biāo)志位,此處表示在活動(dòng)和服務(wù)進(jìn)行綁定后自動(dòng)創(chuàng)建服務(wù)。

活動(dòng)和服務(wù)的解綁使用方法unbindService。

4、測(cè)試

點(diǎn)擊“綁定服務(wù)”后,如下:

 

同時(shí)會(huì)執(zhí)行ready和play方法,會(huì)在日志中打印出來。

點(diǎn)擊“解綁服務(wù)”后,如下:

總結(jié):使用這種方式綁定服務(wù)的流程如下:

Context的bindService方法--》服務(wù)的onCreate方法--》服務(wù)的onBind方法--》服務(wù)運(yùn)行。

解綁服務(wù)流程如下:

服務(wù)運(yùn)行--》Context的unBindService方法--》服務(wù)的onDestroy方法--》服務(wù)停止。

更多關(guān)于Android組件相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論