天天看點

Service服務學習(SimpleRandomServiceDemo)

Android四大元件:(以下是官方文檔)

1.Activitys:

2.Services:

3.Broadcast Receivers

4.Content Providers

Activities

An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.

Services

A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.

Content providers

Broadcast receivers

什麼是Service:

A service can run in the background to perform work even while the user is in a different application

使用Services的方式:

1.startService();

2.bindService();

官方文檔:

Started

Bound

當然了,要想使用Service肯定是少不了在全局檔案中注冊Service,否則沒權限

小筆記:

很讓我郁悶的是為什麼我的ServiceText工程中的R.java為什麼不自動更新了,導緻我在Activity中定位控件資源時定位不到?     V_V….

還有就是我寫的SimpleRandomServiceDemo為什麼老是強制關閉呢?

Inter Process Communication,IPC(程序間通信,吼吼。),它可以用于解決兩個不同Android應用程式程序之間的調用和通信問題。

Service生命周期 :

調用startService()啟動Service --> onCreate() --> onStart() --> Service正在運作-->Sercice被停止-->onDestroy()-->關閉Service

使用方式有兩種 :

1.啟動方式(不能擷取Service對象,無法調用Service中的方法)

1.1:Context.startService()啟動Servide

1.2:Context.stopService()終止Service或Service.stopSefl()停止Service

1.3:1.2中的終止并未真正終止,除非它所在的Activity調用onDestroy()才算真正終止

2.綁定方式(能擷取Service對象,可以調用Service中的方法,通過Connection實作)

          2.1:Context.bindService()建立服務連結

2.2:Context.unbindService()終止連結

最主要的是:同一個Service可以綁定多個服務連結,這樣可以同時為多個不同的元件提供服務

例:播放MP3暫停

Context.startService();啟動服務

Context.bindService();擷取Service對象儲存播放資訊,再用:暫停函數暫停播放

需要注意的是:在此情況下如何調用Context.stopService()終止播放的話并不能停止Service,需要在所有的服務連結關閉後,Service才能真正停止

Intent廣播消息時候要有一個唯一辨別,此處,Service在全局檔案中注冊時也要一個唯一辨別

<service android:name=".繼承Service的類名">

代碼實作Service後有兩種方式啟動服務:顯式啟動和隐式啟動

1.顯式啟動:

final Intent serviceIntent = new Intent(this,繼承Service的類名.class);

startService(serviceIntent);

2.隐式啟動:

final Intent serviceIntent = new Intent();

serviceIntent.setAction("繼承Service的類_完整路徑");

<intent-filter>

<action android:name="繼承Service的類_完整路徑"/>

</intent-filter>

</service>

無論何種方式 :關閉方式都一樣的:stopService(serviceIntent);……………….吼吼

     本文轉自華華世界 51CTO部落格,原文連結:http://blog.51cto.com/mzh3344258/733361,如需轉載請自行聯系原作者

繼續閱讀