Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android項目之HomeHealth基礎學習1:Service

Android項目之HomeHealth基礎學習1:Service

編輯:關於Android編程

一、 Service簡介

Service是android 系統中的四大組件之一(Activity、Service、BroadcastReceiver、ContentProvider),它跟Activity的級別差不多,但不能自己運行只能後台運行,並且可以和其他組件進行交互。service可以在很多場合的應用中使用,比如播放多媒體的時候用戶啟動了其他Activity這個時候程序要在後台繼續播放,比如檢測SD卡上文件的變化,再或者在後台記錄你地理信息位置的改變等等,總之服務總是藏在後台的。

Service的啟動有兩種方式:context.startService() context.bindService()


二、 Service啟動流程

context.startService() 啟動流程:

context.startService() -> onCreate() -> onStart() -> Service running -> context.stopService() -> onDestroy() -> Service stop


如果Service還沒有運行,則android先調用onCreate(),然後調用onStart();

如果Service已經運行,則只調用onStart(),所以一個Service的onStart方法可能會重復調用多次。

如果stopService的時候會直接onDestroy,如果是調用者自己直接退出而沒有調用stopService的話,Service會一直在後台運行,該Service的調用者再啟動起來後可以通過stopService關閉Service。

所以調用startService的生命周期為:onCreate --> onStart (可多次調用) --> onDestroy


context.bindService()啟動流程:

context.bindService() -> onCreate() -> onBind() -> Service running -> onUnbind() -> onDestroy() -> Service stop

onBind()將返回給客戶端一個IBind接口實例,IBind允許客戶端回調服務的方法,比如得到Service的實例、運行狀態或其他操作。這個時候把調用者(Context,例如Activity)會和Service綁定在一起,Context退出了,Srevice就會調用onUnbind->onDestroy相應退出。

所以調用bindService的生命周期為:onCreate --> onBind(只一次,不可多次綁定) --> onUnbind --> onDestory。

在Service每一次的開啟關閉過程中,只有onStart可被多次調用(通過多次startService調用),其他onCreate,onBind,onUnbind,onDestory在一個生命周期中只能被調用一次。

\


三、 Service生命周期 <喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjwvcD4KPHA+ClNlcnZpY2W1xMn6w/zW3MbasqKyu8/xQWN0aXZpdHnEx8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9TZXJ2aWNl0tS8sMihz/tTZXJ2aWNlo6zI57n7srvTw7C0xaWjrL/J0tTU2kFjdGl2aXR5tcRPbkNyZWF0ZbrNT25EZXN0cnB5wO/D5sb0tq+6zcihz/tTZXJ2aWNlPC9wPgo8cD4KQW5kcm9pZE1hbmlmZXN0LnhtbM7EvP7I58/Co7o8L3A+CjxwcmUgY2xhc3M9"brush:java;">

layout布局文件activity_main.xml



    


MainActivity.java代碼如下:

package com.example.hellowendy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	Button buttonOn, buttonOff;
	String TAG = "ServiceActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		buttonOn = (Button) findViewById(R.id.button_on);
		buttonOff = (Button) findViewById(R.id.button_off);

		buttonOn.setOnClickListener(this);
		buttonOff.setOnClickListener(this);
		Log.v(TAG, "ActivityonCreate");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case (R.id.button_on):
			Log.v(TAG, "ActivitystartService");
			this.startService(new Intent(this, PlayService.class));
			// startService(new Intent(this, PlayService.class));

			break;
		case (R.id.button_off): {
			Log.v(TAG, "ActivitystopService");
			this.stopService(new Intent(this, PlayService.class));
			// stopService(new Intent(this, PlayService.class));

		}
			break;

		default:
			break;

		}

	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.v(TAG, "ActivityonStop");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Log.v(TAG, "ActivityonDestroy");
		super.onDestroy();

	}

}


PlayService.java代碼如下:

/**
 * 
 */
/**
 * @author Administrator
 *
 */
package com.example.hellowendy;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class PlayService extends Service {
	String TAG = "ServiceActivity";
	MediaPlayer mediaPlayer;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Toast.makeText(this, "Play Service Created", Toast.LENGTH_LONG).show();
		Log.v(TAG, "ServiceonCreate");
		
		//TextView txtview;
		
		//txtview = (TextView) findViewById(R.id.txt);
		
		//從本地資源中獲取的歌曲cong.mp3, “匆匆那年”,cong.mp3目錄是hellowendy -> res ->raw -> cong.mp3
		mediaPlayer = MediaPlayer.create(this, R.raw.cong);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		//super.onStart(intent, startId);
		//super.onStart();
		/*
		 * //可以在OnCreate裡面創建與音樂的鏈接,也可以在OnStart裡面創建 mediaPlayer =
		 * MediaPlayer.create(this, R.raw.test);
		 */
		Toast.makeText(this, "Play Service onStart", Toast.LENGTH_LONG).show();
		Log.v(TAG, "ServiceonStart");
		mediaPlayer.start();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Toast.makeText(this, "Play Service Stopped", Toast.LENGTH_LONG).show();
		Log.v(TAG, "ServiconDestroy");
		mediaPlayer.stop();
	}

}







  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved