国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中Service服務詳解(二)

2020-04-11 10:58:37
字體:
來源:轉載
供稿:網友

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

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

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

1、修改Service服務類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 {  //創建自己的綁定服務業務邏輯  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, "服務的onBind方法被調用", Toast.LENGTH_SHORT).show();    return musicBinder;  }  /**   * 服務第一次創建的時候調用   */  @Override  public void onCreate() {    super.onCreate();    Toast.makeText(this, "服務的onCreate方法被調用", Toast.LENGTH_SHORT).show();  }  /**   * 服務每一次啟動的時候調用   */  @Override  public int onStartCommand(Intent intent, int flags, int startId) {    Toast.makeText(this, "服務的onStartCommand方法被調用", Toast.LENGTH_SHORT).show();    return super.onStartCommand(intent, flags, startId);  }  @Override  public void onDestroy() {    Toast.makeText(this, "服務的onDestroy方法被調用", Toast.LENGTH_SHORT).show();    super.onDestroy();  }}

在服務類中,添加了內部類MusicBinder,在該內部類中,我們模擬了兩個方法。同時在onBind方法中返回我們內部類實例。

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="啟動服務" />  <Button    android:id="@+id/button2"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="停止服務" />  <Button    android:id="@+id/button3"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="綁定服務" />  <Button    android:id="@+id/button4"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="解綁服務" /></LinearLayout>

即添加了兩個按鈕:“綁定服務”和“解綁服務”按鈕。

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;  //創建ServiceConnection,在綁定服務的時候會用到。  private ServiceConnection connection = new ServiceConnection() {    @Override    public void onServiceDisconnected(ComponentName service) {    }    @Override    public void onServiceConnected(ComponentName name, IBinder service) {      //類型轉換      musicBinder = (MyService.MusicBinder) service;      //指揮服務需要做的工作      musicBinder.ready();      musicBinder.play();    }  };  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //獲取開啟服務按鈕    startService_Button = (Button) findViewById(R.id.button1);    //獲取停止服務按鈕    stopService_Button = (Button) findViewById(R.id.button2);    //獲取綁定服務按鈕    bindService_Button = (Button) findViewById(R.id.button3);    //獲取解綁服務按鈕    unbindService_Button = (Button) findViewById(R.id.button4);    //調用點擊事件    startService_Button.setOnClickListener(this);    stopService_Button.setOnClickListener(this);    bindService_Button.setOnClickListener(this);    unbindService_Button.setOnClickListener(this);  }  /**   * 點擊事件   */  @Override  public void onClick(View view) {    switch(view.getId()){    case R.id.button1:      //“開啟服務”按鈕      Intent startIntent = new Intent(this,MyService.class);      //開啟服務      startService(startIntent);      break;    case R.id.button2:      //“停止服務”按鈕      Intent stopIntent = new Intent(this,MyService.class);      //停止服務      stopService(stopIntent);      break;    case R.id.button3:      //“綁定服務”按鈕      Intent bindIntent = new Intent(this,MyService.class);      bindService(bindIntent, connection, BIND_AUTO_CREATE);      break;    case R.id.button4:      //“解綁服務”按鈕      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;  }}

在該類中,創建了ServiceConnection的匿名類,即當活動和服務建立連接后,需要做的工作,在onServiceConnected方法中我們進行了綁定服務的類型轉換,然后調用相應的業務邏輯方法。

活動和服務的綁定石在onClick方法中實現的:使用bindService方法進行綁定,使MainActivity活動和MyService服務綁定在一起,其中第三個參數是個標志位,此處表示在活動和服務進行綁定后自動創建服務。

活動和服務的解綁使用方法unbindService。

4、測試

點擊“綁定服務”后,如下:

 

同時會執行ready和play方法,會在日志中打印出來。

點擊“解綁服務”后,如下:

總結:使用這種方式綁定服務的流程如下:

Context的bindService方法--》服務的onCreate方法--》服務的onBind方法--》服務運行。

解綁服務流程如下:

服務運行--》Context的unBindService方法--》服務的onDestroy方法--》服務停止。

更多關于Android組件相關內容感興趣的讀者可查看本站專題:《Android基本組件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云浮市| 卢湾区| 凤台县| 江津市| 工布江达县| 武定县| 定安县| 库车县| 城口县| 江川县| 临汾市| 读书| 吉木乃县| 翼城县| 璧山县| 新干县| 卓尼县| 合肥市| 荆州市| 长白| 万年县| 永兴县| 洱源县| 改则县| 南木林县| 怀柔区| 岳普湖县| 会理县| 志丹县| 白河县| 江安县| 金乡县| 武城县| 边坝县| 德惠市| 阳新县| 永城市| 富顺县| 荣成市| 土默特左旗| 车致|