編輯:關於Android編程
本文詳細分析了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程序設計有所幫助。
Android開啟多進程
1. 為何要開啟多進程為何開啟android應用要開啟多進程,主要有以下幾點:單進程所分配的內存不夠,需要更多的內存。在早期android系統只為一個單進程的應用分配了1
Android性能優化系列——Profile GPU Rendering
Profile GPU RenderingAndroid開發者選項中提供了Profile GPU Rendering功能,用於在屏幕上實時顯示GPU渲染每一幀圖像花費的時
Android學習筆記:Home Screen Widgets(1):關於Widget
何為Home Screen Widgets Home screen Widget即稱為小工具或者中文小工具,是顯示在主頁上的views,通過後台進程更新view的數據
Android提高之藍牙傳感應用實例
前面文章介紹了Android利用麥克風采集並顯示模擬信號的實現方法,這種采集手段適用於無IO控制、單純讀取信號的情況。如果傳感器本身需要包含控制電路(例如采集血氧信號需要