Android中Service服務詳解(二)
本文詳細分析了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 {
//創(chuàng)建自己的綁定服務業(yè)務邏輯
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;
}
/**
* 服務第一次創(chuàng)建的時候調用
*/
@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;
//創(chuàng)建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;
}
}
在該類中,創(chuàng)建了ServiceConnection的匿名類,即當活動和服務建立連接后,需要做的工作,在onServiceConnected方法中我們進行了綁定服務的類型轉換,然后調用相應的業(yè)務邏輯方法。
活動和服務的綁定石在onClick方法中實現(xiàn)的:使用bindService方法進行綁定,使MainActivity活動和MyService服務綁定在一起,其中第三個參數(shù)是個標志位,此處表示在活動和服務進行綁定后自動創(chuàng)建服務。
活動和服務的解綁使用方法unbindService。
4、測試
點擊“綁定服務”后,如下:

同時會執(zhí)行ready和play方法,會在日志中打印出來。
點擊“解綁服務”后,如下:

總結:使用這種方式綁定服務的流程如下:
Context的bindService方法--》服務的onCreate方法--》服務的onBind方法--》服務運行。
解綁服務流程如下:
服務運行--》Context的unBindService方法--》服務的onDestroy方法--》服務停止。
更多關于Android組件相關內容感興趣的讀者可查看本站專題:《Android基本組件用法總結》
希望本文所述對大家Android程序設計有所幫助。
- 詳解Android中Service服務的基礎知識及編寫方法
- Android中Service服務詳解(一)
- android開發(fā)教程之開機啟動服務service示例
- Android Service 服務不被殺死的妙招
- 解析Android中如何做到Service被關閉后又自動啟動的實現(xiàn)方法
- Android中實現(xiàn)開機自動啟動服務(service)實例
- android中soap協(xié)議使用(ksoap調用webservice)
- 在Android中 獲取正在運行的Service 實例
- Android中使用IntentService創(chuàng)建后臺服務實例
- Android中的Service相關全面總結
- Android Service服務不被停止詳解及實現(xiàn)
相關文章
Kotlin注解實現(xiàn)Parcelable序列化流程詳解
有時我們會在界面跳轉的過程中,做對象傳值,這時就需要對該對象做序列化處理了。Android中對對象的序列化處理有兩種方式,這篇文章主要介紹了Kotlin注解實現(xiàn)Parcelable序列化2022-12-12
Android?registerForActivityResult新用法實現(xiàn)兩個Activity間數(shù)據傳遞
這篇文章主要為大家介紹了Android?registerForActivityResult新用法實現(xiàn)兩個Activity間數(shù)據傳遞詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
AFURLSessionManager 上傳下載使用代碼說明
本文通過代碼給大家介紹了AFURLSessionManager 上傳下載使用說明,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-09-09
Android應用UI開發(fā)中Fragment的常見用法小結
這篇文章主要介紹了Android應用UI開發(fā)中Fragment的常見用法小結,Fragment的存在是為了解決不同屏幕分辯率的動態(tài)和靈活UI設計,需要的朋友可以參考下2016-02-02
Android中實現(xiàn)記事本動態(tài)添加行效果
記事本對我們每個人來說再熟悉不過,下面這篇文章主要給大家介紹了在Android中實現(xiàn)記事本動態(tài)添加行效果的相關資料,這是最近在開發(fā)中遇到的一個小需求,想著分享出來供大家參考學習,需要的朋友們下面來一起看看吧。2017-06-06

