14天學會安卓開發(第六天)Android Service
【原文:http://blog.csdn.net/corder_raine/article/details/8310126】
目錄
第六天.Android Service
6.1Service概述
6.1.1 Service概念及用途
服務是執行在後臺的一段程式碼。
不是程式,也不是執行緒。
可以執行在它自己的程式,也可以執行在其他應用程式程式的上下文(context)裡面,這取決於自身的需要。
Android中的服務,它與Activity不同,它是不能與使用者互動的,不能自己啟動的,執行在後臺的程式。
媒體播放器的服務,當使用者退出媒體選擇使用者介面,仍然希望音樂依然可以繼續播放,這就是由服務(service)來保證當使用者介面關閉時音樂繼續播放的。
比如當我們一個應用的資料是通過網路獲取的,不同時間的資料是不同的,這時候我們可以用Service在後臺定時更新,而不用每開啟應用的時候在去獲取。
6.2 Service生命週期
onCreate()
在服務被建立時呼叫,該方法只會被呼叫一次,無論呼叫多少次startService()或bindService()方法,服務也只被建立一次。
onStart()
只有採用Context.startService()方法啟動服務時才會回撥該方法。該方法在服務開始執行時被呼叫。多次呼叫startService()方法儘管不會多次建立服務,但onStart() 方法會被多次呼叫。
onDestroy()
服務被終止時呼叫。
onBind()
只有採用Context.bindService()方法啟動服務時才會回撥該方法。該方法在呼叫者與服務繫結時被呼叫,當呼叫者與服務已經繫結,多次呼叫Context.bindService()方法並不會導致該方法被多次呼叫。
onUnbind()
只有採用Context.bindService()方法啟動服務時才會回撥該方法。該方法在呼叫者與服務解除繫結時被呼叫。
startService後,即使呼叫startService的程式結束了Service仍然還存在,直到有程式呼叫stopService,或者Service自己自殺(stopSelf())。
bindService後,Service就和呼叫bindService的程式同生共死了,也就是說當呼叫bindService的程式死了,那麼它bind的Service也要跟著被結束,當然期間也可以呼叫unbindservice讓Service結束。
兩種方式混合使用時,比如說你startService了,我bindService了,那麼只有你stopService了而且我也unbindservice了,這個Service才會被結束。
6.3啟動與停止Service
6.3.1 Service開發步驟
第一步:繼承Service類
1
2
|
public class
MyService extends Service
{ } |
第二步:在AndroidManifest.xml檔案中的節點裡對服務進行配置:
服務不能自己執行,使用startService()方法啟用服務,呼叫者與服務之間沒有關連,即使呼叫者退出了,服務仍然執行。使用bindService()方法啟用服務,呼叫者與服務繫結在了一起,呼叫者一旦退出,服務也就終止,大有“不求同時生,必須同時死”的特點。
如果打算採用Context.startService()方法啟動服務,在服務未被建立時,系統會先呼叫服務的onCreate()方法,接著呼叫onStart()方法。如果呼叫startService()方法前服務已經被建立,多次呼叫startService()方法並不會導致多次建立服務,但會導致多次呼叫onStart()方法。採用startService()方法啟動的服務,只能呼叫Context.stopService()方法結束服務,服務結束時會呼叫onDestroy()方法。
如果打算採用Context.bindService()方法啟動服務,在服務未被建立時,系統會先呼叫服務的onCreate()方法,接著呼叫onBind()方法。這個時候呼叫者和服務繫結在一起,呼叫者退出了,系統就會先呼叫服務的onUnbind()方法,接著呼叫onDestroy()方法。如果呼叫bindService()方法前服務已經被繫結,多次呼叫bindService()方法並不會導致多次建立服務及繫結(也就是說onCreate()和onBind()方法並不會被多次呼叫)。如果呼叫者希望與正在繫結的服務解除繫結,可以呼叫unbindService()方法,呼叫該方法也會導致系統呼叫服務的onUnbind()-->onDestroy()方法。
6.3.2 採用startService()啟動服務
採用Context.startService()方法啟動服務的程式碼如下:
01
02
03
04
05
06
07
08
09
10
11
12
|
public class
HelloActivity extends Activity
{ public void
onCreate(Bundle savedInstanceState) { ...... Button
button =(Button) this .findViewById(R.id.button); button.setOnClickListener( new View.OnClickListener(){ public void
onClick(View v) { Intent
intent = new Intent(HelloActivity. this ,
SMSService. class ); startService(intent); }}); } } |
6.3.3 採用bindService()啟動服務
採用Context.startService()方法啟動服務的程式碼如下:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
public class
HelloActivity extends Activity
{ ServiceConnection
conn = new ServiceConnection()
{ public void
onServiceConnected(ComponentName name, IBinder service) { } public void
onServiceDisconnected(ComponentName name) { } }; public void
onCreate(Bundle savedInstanceState) { Button
button =(Button) this .findViewById(R.id.button); button.setOnClickListener( new View.OnClickListener(){ public void
onClick(View v) { Intent
intent = new Intent(HelloActivity. this ,
SMSService. class ); bindService(intent,
conn, Context.BIND_AUTO_CREATE); //unbindService(conn);//解除繫結 }}); } } |
6.3.4 Service服務演示
1.新建一個Android工程ServiceDemo
2.修改main.xml程式碼,增加二個按鈕
3.新建一個Service,命名為MyService.java
4.新建ServiceDemo.java
5.配置AndroidManifest.xml
6.執行上述工程, 用Logcat檢視日誌
7.按HOME鍵進入Settings(設定)àApplications(應用)àRunningServices(正在執行的服務)
main.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
<? xml version = "1.0" encoding = "utf-8" ?> 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" />
</ LinearLayout > |
MyService.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
public class
MyService extends Service
{ //定義個一個Tag標籤 private static
final
String TAG = "MyService" ; //一個Binder類,用在onBind()
方法裡,這樣Activity那邊可以獲取到 private MyBinder
mBinder = new MyBinder();
public IBinder
onBind(Intent intent) { Log.e(TAG, "start
IBinder~~~" ); return mBinder;
} public void
onCreate() { Log.e(TAG, "start
onCreate~~~" ); super .onCreate(); } public void
onStart(Intent intent, int startId)
{ Log.e(TAG, "start
onStart~~~" ); super .onStart(intent,
startId); } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
public void
onDestroy() { Log.e(TAG, "start
onDestroy~~~" ); super .onDestroy(); } public boolean
onUnbind(Intent intent) { Log.e(TAG, "start
onUnbind~~~" ); return super .onUnbind(intent); } public String
getSystemTime(){ Time
t = new Time();
t.setToNow(); return t.toString();
} public class
MyBinder extends Binder{
MyService
getService() { return MyService. this ; } }
} |
ServiceDemo.java
01
02
03
04
05
06
07
08
09
10
11
12
13
|
public class
ServiceDemo extends Activity
implements OnClickListener
{ private MyService
mMyService; private TextView
mTextView; private Button
startServiceButton; private Button
stopServiceButton; private Context
mContext; public void
onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } |
01
02
03
04
05
06
07
08
09
10
11
|
public void
setupViews(){ mContext
= ServiceDemo. this ; mTextView
= (TextView)findViewById(R.id.text); startServiceButton
= (Button)findViewById(R.id.startservice); stopServiceButton
= (Button)findViewById(R.id.stopservice); startServiceButton.setOnClickListener( this ); stopServiceButton.setOnClickListener( this ); } |
01
02
03
04
05
06
07
08
09
10
11
12
|
public void
onClick(View v) { 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); } } } |
AndroidManifest.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
|
package = "com.lxt008" android:versionCode = "1" android:versionName = "1.0" > < application android:icon = "@drawable/icon" android:label = "@string/app_name" > < activity android:name = ".ServiceDemo" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" />
< category android:name = "android.intent.category.LAUNCHER" />
</ intent-filter > </ activity > < service android:name = ".MyService" android:exported = "true" ></ service > </ application > < uses-sdk android:minSdkVersion = "7" />
</ manifest > |
6.4Notification通知
如果需要檢視訊息,可以拖動狀態列到螢幕下方即可檢視訊息。
傳送訊息的程式碼如下:
01
02
03
04
05
06
07
08
09
10
11
12
13
|
//獲取通知管理器 NotificationManager
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon
= android.R.drawable.stat_notify_chat; long when
= System.currentTimeMillis(); //新建一個通知,指定其圖示和標題 //第一個引數為圖示,第二個引數為標題,第三個為通知時間 Notification
notification = new Notification(icon,
null ,
when); Intent
openintent = new Intent( this ,
OtherActivity. class ); //當點選訊息時就會向系統傳送openintent意圖 PendingIntent
contentIntent = PendingIntent.getActivity( this , 0 ,
openintent, 0 ); notification.setLatestEventInfo( this ,
“標題”, “我是內容", contentIntent); mNotificationManager.notify( 0 ,
notification); |
6.4.1 Android中的通知(Notification)
6.5案例分析
參考案例:NotificationDemo
7個demo以打包
包括
ch08_serviceactivity
ch08_servicelifecycle
ch08_thread
ch08_timer
NotificationDemo
ServiceActivity
ServiceDemo
原始碼下載
相關文章
- 安卓(Android)開發基礎知識安卓Android
- 自學安卓app開發(一)安卓APP
- 【音視訊安卓開發 (十一)】Android初級開發(一)安卓Android
- Android初學路上會遇到的瓶頸【安卓巴士博文大賽】Android安卓
- 安卓開發--AIDL研究安卓AI
- 安卓開發學習-按鈕控制元件安卓控制元件
- 安卓開發學習-Intent攜帶資料安卓Intent
- Android Studio - 安卓開發工具 開啟後報錯集合、修復指南Android安卓
- 安卓開發框架系列開篇安卓框架
- 【精華】安卓開發學習路線規劃安卓
- 安卓開發日記4安卓
- go語言安卓開發Go安卓
- 安卓開發日記28安卓
- 安卓開發日記27安卓
- 安卓開發日記14安卓
- 安卓開發日記13安卓
- 安卓開發日記12安卓
- 安卓開發日記17安卓
- 安卓開發日記16安卓
- 安卓開發日記15安卓
- 安卓開發日記26安卓
- 安卓開發日記25安卓
- 安卓開發日記24安卓
- 安卓開發日記19安卓
- 安卓開發日記18安卓
- 安卓開發日記57安卓
- 安卓開發日記56安卓
- 安卓開發日記55安卓
- 安卓開發日記46安卓
- 安卓開發日記45安卓
- 安卓開發日記47安卓
- 安卓開發第一步:安卓面試題安卓面試題
- 2020年學習安卓開發的前途怎麼樣?安卓
- 安卓開發小組的反思安卓
- archlinux配置安卓開發環境Linux安卓開發環境
- 5.11安卓開發日記32安卓
- 5.9安卓開發日記31安卓
- 5.12安卓開發日記33安卓
- 5.13安卓開發日記34安卓