Android Service生命周期及用法_第1頁
Android Service生命周期及用法_第2頁
Android Service生命周期及用法_第3頁
Android Service生命周期及用法_第4頁
Android Service生命周期及用法_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

AndroidService生命周期及用法!

\o"收藏到我的網(wǎng)摘中,并分享給我的朋友"收藏大家好,上一節(jié)我講解了AndroidActivity的生命周期,這一節(jié)我將講解一下Service,首先我們要知道Service具體是干什么的,什么時候用到?以及它的生命周期等。Service概念及用途:Android中的服務(wù),它與Activity不同,它是不能與用戶交互的,不能自己啟動的,運行在后臺的程序,如果我們退出應(yīng)用時,Service進程并沒有結(jié)束,它仍然在后臺運行,那我們什么時候會用到Service呢?比如我們播放音樂的時候,有可能想邊聽音樂邊干些其他事情,當(dāng)我們退出播放音樂的應(yīng)用,如果不用Service,我們就聽不到歌了,所以這時候就得用到Service了,又比如當(dāng)我們一個應(yīng)用的數(shù)據(jù)是通過網(wǎng)絡(luò)獲取的,不同時間(一段時間)的數(shù)據(jù)是不同的這時候我們可以用Service在后臺定時更新,而不用每打開應(yīng)用的時候在去獲取。Service生命周期

:AndroidService的生命周期并不像Activity那么復(fù)雜,它只繼承了onCreate(),onStart(),onDestroy()三個方法,當(dāng)我們第一次啟動Service時,先后調(diào)用了onCreate(),onStart()這兩個方法,當(dāng)停止Service時,則執(zhí)行onDestroy()方法,這里需要注意的是,如果Service已經(jīng)啟動了,當(dāng)我們再次啟動Service時,不會在執(zhí)行onCreate()方法,而是直接執(zhí)行onStart()方法,具體的可以看下面的實例。Service與Activity通信:Service后端的數(shù)據(jù)最終還是要呈現(xiàn)在前端Activity之上的,因為啟動Service時,系統(tǒng)會重新開啟一個新的進程,這就涉及到不同進程間通信的問題了(AIDL)這一節(jié)我不作過多描述,當(dāng)我們想獲取啟動的Service實例時,我們可以用到bindService和onBindService方法,它們分別執(zhí)行了Service中IBinder()和onUnbind()方法。為了讓大家更容易理解,我寫了一個簡單的Demo,大家可以模仿著我,一步一步的來。第一步:新建一個Android工程,我這里命名為ServiceDemo.第二步:修改main.xml代碼,我這里增加了四個按鈕,代碼如下:viewplaincopytoclipboardprint?<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:id="@+id/text"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<Button

android:id="@+id/startservice"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="startService"

/>

<Button

android:id="@+id/stopservice"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="stopService"

/>

<Button

android:id="@+id/bindservice"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="bindService"

/>

<Button

android:id="@+id/unbindservice"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="unbindService"

/>

</LinearLayout>

第三步:新建一個Service,命名為MyService.java代碼如下:viewplaincopytoclipboardprint?package

com.tutor.servicedemo;

import

android.app.Service;

import

android.content.Intent;

import

android.os.Binder;

import

android.os.IBinder;

import

android.text.format.Time;

import

android.util.Log;

public

class

MyService

extends

Service

{

//定義個一個Tag標(biāo)簽

private

static

final

String

TAG

=

"MyService";

//這里定義吧一個Binder類,用在onBind()有方法里,這樣Activity那邊可以獲取到

private

MyBinder

mBinder

=

new

MyBinder();

@Override

public

IBinder

onBind(Intent

intent)

{

Log.e(TAG,

"start

IBinder~~~");

return

mBinder;

}

@Override

public

void

onCreate()

{

Log.e(TAG,

"start

onCreate~~~");

super.onCreate();

}

@Override

public

void

onStart(Intent

intent,

int

startId)

{

Log.e(TAG,

"start

onStart~~~");

super.onStart(intent,

startId);

}

@Override

public

void

onDestroy()

{

Log.e(TAG,

"start

onDestroy~~~");

super.onDestroy();

}

@Override

public

boolean

onUnbind(Intent

intent)

{

Log.e(TAG,

"start

onUnbind~~~");

return

super.onUnbind(intent);

}

//這里我寫了一個獲取當(dāng)前時間的函數(shù),不過沒有格式化就先這么著吧

public

String

getSystemTime(){

Time

t

=

new

Time();

t.setToNow();

return

t.toString();

}

public

class

MyBinder

extends

Binder{

MyService

getService()

{

return

MyService.this;

}

}

}

第四步:修改ServiceDemo.java,代碼如下:viewplaincopytoclipboardprint?package

com.tutor.servicedemo;

import

android.app.Activity;

import

android.content.ComponentName;

import

android.content.Context;

import

android.content.Intent;

import

android.content.ServiceConnection;

import

android.os.Bundle;

import

android.os.IBinder;

import

android.view.View;

import

android.view.View.OnClickListener;

import

android.widget.Button;

import

android.widget.TextView;

public

class

ServiceDemo

extends

Activity

implements

OnClickListener{

private

MyService

mMyService;

private

TextView

mTextView;

private

Button

startServiceButton;

private

Button

stopServiceButton;

private

Button

bindServiceButton;

private

Button

unbindServiceButton;

private

Context

mContext;

//這里需要用到ServiceConnection在Context.bindService和context.unBindService()里用到

private

ServiceConnection

mServiceConnection

=

new

ServiceConnection()

{

//當(dāng)我bindService時,讓TextView顯示MyService里getSystemTime()方法的返回值

public

void

onServiceConnected(ComponentName

name,

IBinder

service)

{

//

TODO

Auto-generated

method

stub

mMyService

=

((MyService.MyBinder)service).getService();

mTextView.setText("I

am

frome

Service

:"

+

mMyService.getSystemTime());

}

public

void

onServiceDisconnected(ComponentName

name)

{

//

TODO

Auto-generated

method

stub

}

};

public

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

setupViews();

}

public

void

setupViews(){

mContext

=

ServiceDemo.this;

mTextView

=

(TextView)findViewById(R.id.text);

startServiceButton

=

(Button)findViewById(R.id.startservice);

stopServiceButton

=

(Button)findViewById(R.id.stopservice);

bindServiceButton

=

(Button)findViewById(R.id.bindservice);

unbindServiceButton

=

(Button)findViewById(R.id.unbindservice);

startServiceButton.setOnClickListener(this);

stopServiceButton.setOnClickListener(this);

bindServiceButton.setOnClickListener(this);

unbindServiceButton.setOnClickListener(this);

}

public

void

onClick(View

v)

{

//

TODO

Auto-generated

method

stub

if(v

==

startServiceButton){

Intent

i

=

new

Intent();

i.setClass(ServiceDemo.this,

MyService.class);

mContext.startService(i);

}else

if(v

==

stopServiceButton){

Intent

i

=

new

Intent();

i.setClass(ServiceDemo.this,

MyService.class);

mContext.stopService(i);

}else

if(v

==

bindServiceButton){

Intent

i

=

new

Intent();

i.setClass(ServiceDemo.this,

MyService.class);

mContext.bindService(i,

mServiceConnection,

BIND_AUTO_CREATE);

}else{

mContext.unbindService(mServiceConnection);

}

}

}

第五步:修改AndroidManifest.xml代碼(將我們新建的MyService注冊進去如下代碼第14行:)

viewplaincopytoclipboardprint?<?xml

version="1.0"

encoding="utf-8"?>

<manifest

xmlns:android="/apk/res/android"

package="com.tutor.servicedemo"

android:versionCode="1"

android:versionName="1.0">

<application

android:icon="@drawable/icon"

android:label="@string/app_name">

<activity

android:name=".ServiceDemo"

android:label="

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論