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

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

Anroid四大組件service之本地服務(wù)的示例代碼

2019-10-22 18:25:15
字體:
供稿:網(wǎng)友

服務(wù)是Android四大組件之一,與Activity一樣,代表可執(zhí)行程序。但Service不像Activity有可操作的用戶界面,它是一直在后臺(tái)運(yùn)行。用通俗易懂點(diǎn)的話來說:

如果某個(gè)應(yīng)用要在運(yùn)行時(shí)向用戶呈現(xiàn)可操作的信息就應(yīng)該選擇Activity,如果不是就選擇Service。

Service的生命周期如下:

Service只會(huì)被創(chuàng)建一次,也只會(huì)被銷毀一次。那么,如何創(chuàng)建本地服務(wù)呢?

實(shí)現(xiàn)代碼如下:

package temp.com.android/285863.html">androidserivce;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.SystemClock;import android.support.annotation.Nullable;import android.util.Log;/** * Created by Administrator on 2017/8/18. */public class Myservice extends Service {  @Override  public void onCreate() {    Log.i("test", "服務(wù)被創(chuàng)建");    super.onCreate();  }  @Override  public int onStartCommand(Intent intent, int flags, int startId) {    Log.i("test", "服務(wù)被啟動(dòng)");    new Thread(new myRunnable(startId)).start();    return super.onStartCommand(intent, flags, startId);  }  @Override  public void onDestroy() {    Log.i("test", "服務(wù)被銷毀");    super.onDestroy();  }  @Nullable  @Override  public IBinder onBind(Intent intent) {    return null;  }  class myRunnable implements Runnable {    int startId;    public myRunnable(int startId) {      this.startId = startId;    }    @Override    public void run() {      for (int i = 0; i < 10; i++) {        SystemClock.sleep(1000);        Log.i("test", i + "");       }      //停止服務(wù)      //stopSelf();      stopSelf(startId);      //當(dāng)用無參數(shù)的停止服務(wù)時(shí),將會(huì)銷毀第一次所啟動(dòng)的服務(wù);      //當(dāng)用帶參數(shù)的停止服務(wù)時(shí),將會(huì)銷毀最末次所啟動(dòng)的服務(wù);    }  }}

要聲明服務(wù),就必須在manifests中進(jìn)行配置

<manifest ... > ... <application ... >   <service android:name=".Myservice" android:exported="true"/> ... </application> </manifest>

android:exported="true" 設(shè)置了這個(gè)屬性就表示別人也可以使用你的服務(wù)。

還有一個(gè)需要注意的小點(diǎn),在Myservice中可以看見我啟動(dòng)時(shí)用了一個(gè)子線程去幫我實(shí)現(xiàn)工作,那么我為什么沒有直接把for循環(huán)的那段代碼寫在onStartCommand方法中呢,是因?yàn)閷懺趏nStartCommand中將會(huì)報(bào)ANR程序無響應(yīng)的錯(cuò)誤。就是當(dāng)你所有的事情都去交給主線程做時(shí),就會(huì)造成主線程內(nèi)存溢出,它就會(huì)炸了。這個(gè)時(shí)候也可以用IntentService來取代Service。

package temp.com.androidserivce;import android.app.IntentService;import android.content.Intent;import android.os.SystemClock;import android.util.Log;/** * Created by Administrator on 2017/8/18. */public class MyService2 extends IntentService {  public MyService2() {    super("");  }  public MyService2(String name) {    super(name);  }  @Override  protected void onHandleIntent(Intent intent) {    for (int i = 0; i <10 ; i++) {      SystemClock.sleep(1000);      Log.i("test",i+"");    }  }}

使用這個(gè)相對(duì)而言會(huì)比較簡(jiǎn)單。IntentService是Service的子類。它使用工作線程逐一處理所有啟動(dòng)請(qǐng)求。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桃江县| 柞水县| 龙门县| 定州市| 周至县| 唐山市| 延寿县| 定兴县| 绵竹市| 滁州市| 广河县| 阳新县| 泰兴市| 保靖县| 敦化市| 绥滨县| 瑞丽市| 镇原县| 辛集市| 潢川县| 桐柏县| 武定县| 翁源县| 和平县| 尼玛县| 连江县| 博白县| 邮箱| 三明市| 石狮市| 沁源县| 茶陵县| 山丹县| 漾濞| 兴城市| 漠河县| 龙川县| 息烽县| 衡阳县| 合作市| 台北县|