Android中Service(后臺服務)詳解
1.概念:
(1).Service可以說是一個在后臺運行的Activity。它不是一個單獨的進程,它只需要應用告訴它要在后臺做什么就可以了。
(2).它要是實現(xiàn)和用戶的交互的話需要通過通知欄或者是通過發(fā)送廣播,UI去接收顯示。
(3).它的應用十分廣泛,尤其是在框架層,應用更多的是對系統(tǒng)服務的調(diào)用。
2.作用:
(1).它用于處理一些不干擾用戶使用的后臺操作。如下載,網(wǎng)絡獲取。播放音樂,他可以通過INTENT來開啟,同時也可以綁定到宿主對象(調(diào)用者例如ACTIVITY上)來使用。
(2).如果說Activity是顯示前臺頁面的信息,那么Service就是在后臺進行操作的。如果Service和前臺UI進行交互的話可以通過發(fā)送廣播或者通知欄的方式。
3.生命周期:
(1).service整體的生命時間是從onCreate()被調(diào)用開始,到onDestroy()方法返回為止。和activity一樣,service在onCreate()中進行它的初始化工作,在onDestroy()中釋放殘留的資源。
(2).**startService()的方式:**onCreate()->onStartCommand()->onStart()->onDestroy()
(3).**BindService()的方式:**onCreate()->onBinder()->onUnbind()->onDestroy()。onUnbind()方法返回后就結(jié)束了。
4.啟動方式:
(1).Service自己不能運行,需要通過某一個Activity或者其它Context對象來調(diào)用。
(2).Service的啟動方式有兩種:
Context.startService()和Context.bindService()兩種方式啟動Service。如果在service的onCreate()方法或者onStart()方法中有耗時的操作,要新開啟一個線程。 在需要service的地方通過以上兩種方式來啟動。
注意:
平常使用多的是startService方法,可以把一些耗時的任務放到后臺去處理,當處理完成后,可以通過廣播或者通知欄來通知前臺。
5.以下通過代碼來深入理解:
(1).MainActivity.java類:
package com.example.servicetest;
import com.example.servicetest.service.MyService;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
/** 標志位 */
private static String TAG = "com.example.servicetest.MainActivity";
/** 啟動服務 */
private Button mBtnStart;
/** 綁定服務 */
private Button mBtnBind;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* init the View
*/
private void initView() {
mBtnStart = (Button) findViewById(R.id.startservice);
mBtnBind = (Button) findViewById(R.id.bindservice);
mBtnStart.setOnClickListener(this);
mBtnBind.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
// 啟動服務的方式
case R.id.startservice:
startService(new Intent(MyService.ACTION));
break;
// 綁定服務的方式
case R.id.bindservice:
bindService(new Intent(MyService.ACTION), conn, BIND_AUTO_CREATE);
break;
default:
break;
}
}
ServiceConnection conn = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
Log.v(TAG, "onServiceConnected");
}
public void onServiceDisconnected(ComponentName name) {
Log.v(TAG, "onServiceDisconnected");
}
};
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("-------onDestroy()--");
stopService(new Intent(MyService.ACTION));
unbindService(conn);
}
}
(2).MyService.java類:
package com.example.servicetest.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
/** 標志位 */
private static String TAG = "com.example.servicetest.service.MyService";
/** 行為 */
public static final String ACTION = "com.example.servicetest.service.MyService";
@Override
public void onCreate() {
super.onCreate();
System.out.println("----- onCreate() ---");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
System.out.println("----- onStart() ---");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("----- onStartCommand() ---");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
System.out.println("----- onBind() ---");
return null;
}
@Override
public void onRebind(Intent intent) {
System.out.println("----- onRebind() ---");
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("----- onUnbind() ---");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
System.out.println("----- onDestroy() ---");
super.onDestroy();
}
}
(3).AndroidManifest.xml
<!-- 注冊 -->
<service android:name="com.example.servicetest.service.MyService" >
<intent-filter>
<!-- 用來啟動服務的Intent -->
<action android:name="com.example.servicetest.service.MyService" />
<category android:name="android.intent.category.default" />
</intent-filter>
</service>

StartService方法:
(1).當按startService按鈕的時候:如下:

(2).再繼續(xù)按startService按鈕的時候:如下:

BindService方法:
(1).當按bindService按鈕的時候:如下:

(2).再繼續(xù)按bindService按鈕的時候:如下:

(3).先按startService按鈕再按bindService按鈕:如下:

相關(guān)文章
Android 讓自定義TextView的drawableLeft與文本一起居中
本文主要介紹Android 自定義控件TextView顯示居中問題,在開發(fā)過程中經(jīng)常會遇到控件的重寫,這里主要介紹TextView的drawableLeft與文本一起居中的問題2016-07-07
Android 設(shè)置Edittext獲取焦點并彈出軟鍵盤
本文主要介紹了Android設(shè)置Edittext獲取焦點并彈出軟鍵盤的實現(xiàn)代碼。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
Android 優(yōu)雅的實現(xiàn)通用格式化編輯
這篇文章主要介紹了Android 優(yōu)雅的實現(xiàn)通用格式化編輯,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下2021-03-03
Android仿拉手網(wǎng)團購App我的收藏界面實例代碼
這篇文章主要介紹了Android仿拉手團購網(wǎng)App我的收藏界面實例代碼,需要的朋友可以參考下2017-05-05
android教程之intent的action屬性使用示例(intent發(fā)短信)
這篇文章主要介紹了android中intent的action屬性使用示例,提供了使用intent撥打電話、發(fā)送短信、播放mp3的代碼2014-01-01
教你制作Android中炫酷的ViewPagerIndicator(不僅仿MIUI)
ViewPagerIndicator作為一款分頁指標小部件兼容ViewPager,封裝上做得非常不錯,目前已為眾多知名應用所使用。今天給你大家分享一個炫酷的效果,有需要的可以參考學習。2016-08-08
Jetpack?Compose實現(xiàn)對角線滾動效果
這篇文章主要為大家詳細介紹了如何利用Jetpack?Compose實現(xiàn)一個簡單的對角線滾動效果,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-02-02

