基于Android Service 生命周期的詳細(xì)介紹
Service概念及用途:
Android中的服務(wù),它與Activity不同,它是不能與用戶交互的,不能自己啟動(dòng)的,運(yùn)行在后臺的程序,如果我們退出應(yīng)用時(shí),Service進(jìn)程并沒有結(jié)束,它仍然在后臺運(yùn)行,那我們什么時(shí)候會用到Service呢?比如我們播放音樂的時(shí)候,有可能想邊聽音樂邊干些其他事情,當(dāng)我們退出播放音樂的應(yīng)用,如果不用Service,我們就聽不到歌了,所以這時(shí)候就得用到Service了,又比如當(dāng)我們一個(gè)應(yīng)用的數(shù)據(jù)是通過網(wǎng)絡(luò)獲取的,不同時(shí)間(一段時(shí)間)的數(shù)據(jù)是不同的這時(shí)候我們可以用Service在后臺定時(shí)更新,而不用每打開應(yīng)用的時(shí)候在去獲取。
Service生命周期 :
Android Service的生命周期并不像Activity那么復(fù)雜,它只繼承了onCreate(),onStart(),onDestroy()三個(gè)方法,當(dāng)我們第一次啟動(dòng)Service時(shí),先后調(diào)用了onCreate(),onStart()這兩個(gè)方法,當(dāng)停止Service時(shí),則執(zhí)行onDestroy()方法,這里需要注意的是,如果Service已經(jīng)啟動(dòng)了,當(dāng)我們再次啟動(dòng)Service時(shí),不會在執(zhí)行onCreate()方法,而是直接執(zhí)行onStart()方法。
Service與Activity通信:
Service后端的數(shù)據(jù)最終還是要呈現(xiàn)在前端Activity之上的,因?yàn)閱?dòng)Service時(shí),系統(tǒng)會重新開啟一個(gè)新的進(jìn)程,這就涉及到不同進(jìn)程間通信的問題了(AIDL),當(dāng)我們想獲取啟動(dòng)的Service實(shí)例時(shí),我們可以用到bindService和unBindService方法,它們分別執(zhí)行了Service中IBinder()和onUnbind()方法。
1、添加一個(gè)類,在MainActivity所在包之下
public class LService extends Service {
private static final String TAG = "LService";
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onbind");
return null;
}
@Override
public void onCreate() {
Log.i(TAG, "oncreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onstart");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
Log.i(TAG, "ondestoty");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onubind");
return super.onUnbind(intent);
}
public String getSystemTime() {
Time t = new Time();
t.setToNow();
return t.toString();
}
public class LBinder extends Binder {
LService getService() {
return LService.this;
}
}
}
2、在程序界面文件中添加控件
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="wecclome to Livingstone's bolg" />
<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" />
3、修改MainActivity中的方法,以及讓MainActivity類實(shí)現(xiàn)OnClickListener接口
public class MainActivity extends Activity implements OnClickListener {
private LService mLService;
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時(shí),讓TextView顯示LService里getSystemTime()方法的返回值
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mLService = ((LService.LBinder) service).getService();
mTextView.setText("I am from Service :" + mLService.getSystemTime());
}
public void onServiceDisconnected(ComponentName name) {
}
};
public void setupViews() {
mContext = MainActivity.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);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
}
@Override
public void onClick(View v) {
if (v == startServiceButton) {
Intent i = new Intent(MainActivity.this, LService.class);
mContext.startService(i);
} else if (v == stopServiceButton) {
Intent i = new Intent(MainActivity.this, LService.class);
mContext.stopService(i);
} else if (v == bindServiceButton) {
Intent i = new Intent(MainActivity.this, LService.class);
mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE);
} else {
mContext.unbindService(mServiceConnection);
}
}
}
4、注冊Service
<service
android:name=".LService"
android:exported="true" >
</service>
5、運(yùn)行程序
程序界面
點(diǎn)擊startService此時(shí)調(diào)用程序設(shè)置里面可以看到Running Service有一個(gè)LService
點(diǎn)擊stopService
點(diǎn)擊bindService此時(shí)Service已經(jīng)被關(guān)閉
點(diǎn)擊unbindService
先點(diǎn)擊startService,再依次點(diǎn)擊bindService和unbindService
- Android基于Service的音樂播放器
- Android通過startService播放背景音樂
- Android中實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)服務(wù)(service)實(shí)例
- Android中的Service相關(guān)全面總結(jié)
- android教程之service使用方法示例詳解
- Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
- android使用Messenger綁定Service的多種實(shí)現(xiàn)方法
- android調(diào)用web service(cxf)實(shí)例應(yīng)用詳解
- Android創(chuàng)建服務(wù)之started service詳細(xì)介紹
- Android基于service實(shí)現(xiàn)音樂的后臺播放功能示例
相關(guān)文章
Android okhttp的啟動(dòng)流程及源碼解析
這篇文章主要介紹了Android okhttp的啟動(dòng)流程及源碼解析,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-03-03全面解析Android應(yīng)用開發(fā)中Activity類的用法
這篇文章主要介紹了Android應(yīng)用開發(fā)中Activity類的用法,包括Activity間的數(shù)據(jù)傳遞以及Activity的創(chuàng)建方式等,需要的朋友可以參考下2016-02-02Android Studio生成函數(shù)注釋的實(shí)現(xiàn)方法
這篇文章主要介紹了Android Studio生成函數(shù)注釋的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文大家能夠?qū)崿F(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09Android Studio獲取SHA1值實(shí)例詳解
這篇文章主要介紹了Android Studio獲取SHA1值實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Android在Sqlite3中的應(yīng)用及多線程使用數(shù)據(jù)庫的建議(實(shí)例代碼)
這篇文章主要介紹了Android在Sqlite3中的應(yīng)用及多線程使用數(shù)據(jù)庫的建議,包括編寫數(shù)據(jù)庫具體操作類、增刪改查,通過實(shí)例代碼介紹了在實(shí)際中的應(yīng)用,需要的朋友可以參考下2022-04-04Android WebView打開網(wǎng)頁一片空白的解決方法
這篇文章主要介紹了Android WebView打開網(wǎng)頁一片空白的解決方法,試了很多方法,最后記錄一下,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12Android11文件管理權(quán)限申請?jiān)敿?xì)介紹
大家好,本篇文章主要講的是Android11文件管理權(quán)限申請?jiān)敿?xì)介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android后臺定時(shí)提醒功能實(shí)現(xiàn)
這篇文章主要介紹了Android后臺定時(shí)提醒功能,針對Service,AlarmManager的使用進(jìn)行詳細(xì)闡述,感興趣的小伙伴們可以參考一下2016-01-01Android編程實(shí)現(xiàn)對話框形式進(jìn)度條功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)對話框形式進(jìn)度條功能,結(jié)合具體實(shí)例形式分析了Android對話框形式進(jìn)度條的功能與布局相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09