最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年10月23日 09:14:28   作者:彭艷  
本篇文章主要介紹了Anroid四大組件service之本地服務(wù)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

Service的生命周期如下:

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

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

package temp.com.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ù)被啟動");
    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);
      //當用無參數(shù)的停止服務(wù)時,將會銷毀第一次所啟動的服務(wù);
      //當用帶參數(shù)的停止服務(wù)時,將會銷毀最末次所啟動的服務(wù);

    }
  }
}

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

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

 ... 

</application>

 </manifest>

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

還有一個需要注意的小點,在Myservice中可以看見我啟動時用了一個子線程去幫我實現(xiàn)工作,那么我為什么沒有直接把for循環(huán)的那段代碼寫在onStartCommand方法中呢,是因為寫在onStartCommand中將會報ANR程序無響應(yīng)的錯誤。就是當你所有的事情都去交給主線程做時,就會造成主線程內(nèi)存溢出,它就會炸了。這個時候也可以用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+"");
    }
  }
}

使用這個相對而言會比較簡單。IntentService是Service的子類。它使用工作線程逐一處理所有啟動請求。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

都兰县| 农安县| 玉田县| 浠水县| 噶尔县| 耿马| 阜新| 乌兰县| 湟中县| 黄陵县| 乌拉特中旗| 无锡市| 辉南县| 顺平县| 永寿县| 札达县| 宣汉县| 临澧县| 沂水县| 蛟河市| 民勤县| 罗定市| 松原市| 西城区| 古蔺县| 余姚市| 大安市| 石河子市| 仲巴县| 分宜县| 图们市| 万荣县| 宜兰县| 惠州市| 石林| 吴川市| 章丘市| 巴林左旗| 百色市| 离岛区| 长泰县|