国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android提高之Service用法實(shí)例解析

2020-04-11 11:46:11
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前面文章介紹了Activity以及Intent的使用,本文就來(lái)介紹Service。如果把Activity比喻為前臺(tái)程序,那么Service就是后臺(tái)程序,Service的整個(gè)生命周期都只會(huì)在后臺(tái)執(zhí)行Service跟Activity一樣也由Intent調(diào)用。在工程里想要添加一個(gè)Service,先新建繼承Service的類,然后到AndroidManifest.xml -> Application ->Application Nodes中的Service標(biāo)簽中添加

 Service要由Activity通過(guò)startService 或者 bindService來(lái)啟動(dòng),Intent負(fù)責(zé)傳遞參數(shù)。
 
 先貼出本文程序運(yùn)行截圖如下:

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

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

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

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

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

上圖是bindService之后再Activity.finish()的Service狀態(tài)變化,Service最后自動(dòng)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"; }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 聂拉木县| 新巴尔虎左旗| 平利县| 麻江县| 满洲里市| 夏河县| 永嘉县| 额敏县| 北碚区| 广宗县| 阿城市| 孟连| 陆良县| 元朗区| 环江| 类乌齐县| 岳阳市| 绥滨县| 双流县| 萍乡市| 陇川县| 颍上县| 榆林市| 伊金霍洛旗| 沙洋县| 德格县| 志丹县| 洛阳市| 南宁市| 平南县| 晋江市| 连城县| 淮安市| 河北省| 许昌市| 唐海县| 北宁市| 明光市| 湘潭县| 岳阳县| 南平市|