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

首頁 > 系統 > Android > 正文

Android中Service(后臺服務)詳解

2020-04-11 11:30:40
字體:
來源:轉載
供稿:網友

1.概念:
(1).Service可以說是一個在后臺運行的Activity。它不是一個單獨的進程,它只需要應用告訴它要在后臺做什么就可以了。
(2).它要是實現和用戶的交互的話需要通過通知欄或者是通過發送廣播,UI去接收顯示。
(3).它的應用十分廣泛,尤其是在框架層,應用更多的是對系統服務的調用。
2.作用:
(1).它用于處理一些不干擾用戶使用的后臺操作。如下載,網絡獲取。播放音樂,他可以通過INTENT來開啟,同時也可以綁定到宿主對象(調用者例如ACTIVITY上)來使用。
(2).如果說Activity是顯示前臺頁面的信息,那么Service就是在后臺進行操作的。如果Service和前臺UI進行交互的話可以通過發送廣播或者通知欄的方式。
3.生命周期:
(1).service整體的生命時間是從onCreate()被調用開始,到onDestroy()方法返回為止。和activity一樣,service在onCreate()中進行它的初始化工作,在onDestroy()中釋放殘留的資源。
(2).**startService()的方式:**onCreate()->onStartCommand()->onStart()->onDestroy()
(3).**BindService()的方式:**onCreate()->onBinder()->onUnbind()->onDestroy()。onUnbind()方法返回后就結束了。

4.啟動方式:
(1).Service自己不能運行,需要通過某一個Activity或者其它Context對象來調用。
(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).再繼續按startService按鈕的時候:如下:

BindService方法:

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

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万载县| 建昌县| 隆德县| 鸡东县| 康平县| 中西区| 饶平县| 行唐县| 榆中县| 河间市| 铜山县| 夏津县| 赞皇县| 东城区| 商南县| 西峡县| 新化县| 大足县| 大竹县| 报价| 江安县| 海门市| 神木县| 龙南县| 岫岩| 揭东县| 迁安市| 从化市| 辽宁省| 弥勒县| 彩票| 永和县| 石首市| 疏勒县| 多伦县| 体育| 鄂州市| 阿瓦提县| 龙陵县| 大冶市| 定日县|