Android提高之Service用法實(shí)例解析
前面文章介紹了Activity以及Intent的使用,本文就來介紹Service。如果把Activity比喻為前臺程序,那么Service就是后臺程序,Service的整個生命周期都只會在后臺執(zhí)行。Service跟Activity一樣也由Intent調(diào)用。在工程里想要添加一個Service,先新建繼承Service的類,然后到AndroidManifest.xml -> Application ->Application Nodes中的Service標(biāo)簽中添加。
Service要由Activity通過startService 或者 bindService來啟動,Intent負(fù)責(zé)傳遞參數(shù)。
先貼出本文程序運(yùn)行截圖如下:

本文主要講解Service的調(diào)用,以及其生命周期。

上圖是startService之后再stopService的Service狀態(tài)變化。

上圖是bindService之后再unbindService的Service狀態(tài)變化。
startService與bindService都可以啟動Service,那么它們之間有什么區(qū)別呢?它們兩者的區(qū)別就是使Service的周期改變。由startService啟動的Service必須要有stopService來結(jié)束Service,不調(diào)用stopService則會造成Activity結(jié)束了而Service還運(yùn)行著。bindService啟動的Service可以由unbindService來結(jié)束,也可以在Activity結(jié)束之后(onDestroy)自動結(jié)束。

上圖是startService之后再Activity.finish()的Service狀態(tài)變化,Service還在跑著。

上圖是bindService之后再Activity.finish()的Service狀態(tài)變化,Service最后自動unbindService。
main.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnStartMyService" android:text="StartMyService"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnStopMyService" android:text="StopMyService"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnBindMyService" android:text="BindMyService"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnUnbindMyService" android:text="UnbindMyService"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnExit" android:text="退出程序"></Button> </LinearLayout>
testService.java的源碼如下:
package com.testService;
import android.app.Activity;
import android.app.Service;
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.widget.Button;
public class testService extends Activity {
Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService);
btnStartMyService.setOnClickListener(new ClickEvent());
btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService);
btnStopMyService.setOnClickListener(new ClickEvent());
btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService);
btnBindMyService.setOnClickListener(new ClickEvent());
btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService);
btnUnbindMyService.setOnClickListener(new ClickEvent());
btnExit=(Button)this.findViewById(R.id.btnExit);
btnExit.setOnClickListener(new ClickEvent());
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.e("Activity","onDestroy");
}
private ServiceConnection _connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
class ClickEvent implements View.OnClickListener{
@Override
public void onClick(View v) {
Intent intent=new Intent(testService.this,MyService.class);
if(v==btnStartMyService){
testService.this.startService(intent);
}
else if(v==btnStopMyService){
testService.this.stopService(intent);
}
else if(v==btnBindMyService){
testService.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE);
}
else if(v==btnUnbindMyService){
if(MyService.ServiceState=="onBind")//Service綁定了之后才能解綁
testService.this.unbindService(_connection);
}
else if(v==btnExit)
{
testService.this.finish();
}
}
}
}
MyService.java的源碼如下:
package com.testService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
static public String ServiceState="";
@Override
public IBinder onBind(Intent arg0) {
Log.e("Service", "onBind");
ServiceState="onBind";
return null;
}
@Override
public boolean onUnbind(Intent intent){
super.onUnbind(intent);
Log.e("Service", "onUnbind");
ServiceState="onUnbind";
return false;
}
@Override
public void onCreate(){
super.onCreate();
Log.e("Service", "onCreate");
ServiceState="onCreate";
}
@Override
public void onDestroy(){
super.onDestroy();
Log.e("Service", "onDestroy");
ServiceState="onDestroy";
}
@Override
public void onStart(Intent intent,int startid){
super.onStart(intent, startid);
Log.e("Service", "onStart");
ServiceState="onStart";
}
}
- android開發(fā)教程之開機(jī)啟動服務(wù)service示例
- 解析Android中如何做到Service被關(guān)閉后又自動啟動的實(shí)現(xiàn)方法
- Android中實(shí)現(xiàn)開機(jī)自動啟動服務(wù)(service)實(shí)例
- Android Service 服務(wù)不被殺死的妙招
- Android中的Service相關(guān)全面總結(jié)
- Android創(chuàng)建服務(wù)之started service詳細(xì)介紹
- Android實(shí)現(xiàn)Service獲取當(dāng)前位置(GPS+基站)的方法
- Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
- Android實(shí)現(xiàn)Service重啟的方法
- Android的Service應(yīng)用程序組件基本編寫方法
- Android中Service(后臺服務(wù))詳解
- Android四大組件之Service(服務(wù))實(shí)例詳解
- Android提高Service優(yōu)先級的方法分析
相關(guān)文章
不允許錯過的Anndroid技術(shù)經(jīng)驗(yàn)60條
不允許錯過的Anndroid技術(shù)經(jīng)驗(yàn)60條,與大家分享,希望可以提高大家Android開發(fā)水平,感興趣的朋友可以參考一下2016-02-02
Android聊天工具基于socket實(shí)現(xiàn)
這篇文章主要介紹了基于socket實(shí)現(xiàn)的一個簡單的Android聊天工具,實(shí)現(xiàn)方法簡單,具有一定的參考價值,感興趣的朋友可以參考一下2016-02-02
Android應(yīng)用開發(fā)中實(shí)現(xiàn)apk皮膚文件換膚的思路分析
這篇文章主要介紹了Android應(yīng)用開發(fā)中實(shí)現(xiàn)apk皮膚文件換膚的思路分析,包括布局和主要的皮膚更換邏輯實(shí)現(xiàn),需要的朋友可以參考下2016-02-02
詳解Android中實(shí)現(xiàn)ListView左右滑動刪除條目的方法
這篇文章主要介紹了Android中實(shí)現(xiàn)ListView左右滑動刪除條目的方法,文中分別展示了通過Scroller和NineOldAndroids來實(shí)現(xiàn)的例子,需要的朋友可以參考下2016-04-04
Kotlin?coroutineContext源碼層深入分析
表示一個元素或者是元素集合的接口。它有一個Key(索引)的Element實(shí)例集合,每一個Element的實(shí)例也是一個CoroutineContext,即集合中每個元素也是集合2022-11-11
Android Studio 3.6 正式版終于發(fā)布了,快來圍觀
Android Studio 3.6 正式版終于發(fā)布了,值得興奮呀,畢竟 3.5 大版本更新也已經(jīng)差不多半年了,撒花撒花!這次更新又更新了什么呢?快來跟隨小編一起看看吧2020-02-02
android開發(fā)基礎(chǔ)教程—三種方式實(shí)現(xiàn)xml文件解析
本文將介紹三種方式:sax方式/dom方式/pull方式實(shí)現(xiàn)xml文件解析,感興趣的朋友可以了解下2013-01-01

