• <noscript id="e0iig"><kbd id="e0iig"></kbd></noscript>
  • <td id="e0iig"></td>
  • <option id="e0iig"></option>
  • <noscript id="e0iig"><source id="e0iig"></source></noscript>
  • android學習筆記之IntentService

    標簽: android

    在學習IntentService之前我們要先說一下started Service。
    先看一個例子:
    我們在Service中模擬一個耗時操作(不手動開啟新線程),并在主Activity中startService,如下

    package com.example.demo;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    
    public class MyService extends Service {
        public MyService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }
        //Service創建時調用
        @Override
        public void onCreate() {
            Log.i("Service","Service創建");
            super.onCreate();
        }
        //Service啟動時調用
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
                    Log.i("Service","Service啟動");
                    Long endTime=System.currentTimeMillis()+20*1000;
                    while(System.currentTimeMillis()<endTime){
                        synchronized (this) {
                            try {
                                wait(endTime - System.currentTimeMillis());//釋放鎖
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    stopSelf();
            return super.onStartCommand(intent, flags, startId);
        }
        //Service停止時調用
        @Override
        public void onDestroy() {
            Log.i("Service","Service涼了");
            super.onDestroy();
        }
    }
    
    

    因為started Service不會自動創建一個新的線程,所以操作都是在主線程的,讓主線程等待,很容易出現ANR(Application Not Responding)錯誤,如下
    在這里插入圖片描述
    而且我們也說過,started service只能通過手動調用的stopSelf()或stopService()的方法來停止。
    ————————————————————————————————————————————
    IntentService在開啟Service時,會自動開啟一個新的線程,而且在Service運行結束后也會自動停止。
    我們用IntentService試一下:

    public class MyIntentService extends IntentService {
        /**
         * Creates an IntentService.  Invoked by your subclass's constructor.
         *
         * @param name Used to name the worker thread, important only for debugging.
         */
        public MyIntentService(String name) {
            super(name);
        }
        public MyIntentService(){
            super("MyIntentService");
    
        }
        @Override
        protected void onHandleIntent(@Nullable Intent intent) {
            Log.i("Service","Service啟動");
            Long endTime=System.currentTimeMillis()+5*1000;
            while(System.currentTimeMillis()<endTime){
                synchronized (this) {
                    try {
                        wait(endTime - System.currentTimeMillis());//釋放鎖
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
            }
        }
    
        @Override
        public void onDestroy() {
            Log.i("Service","Service-->成盒");
            super.onDestroy();
        }
    }
    

    在這里插入圖片描述發現他并沒有出現ANR錯誤(因為他自動開啟了一個新線程),并且調用了onDestory()方法停止了線程
    總結:
    1.IntentService在開啟服務時會自動創建一個新線程
    2.IntentService會自動停止服務

    版權聲明:本文為qq_34423913原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
    本文鏈接:https://blog.csdn.net/qq_34423913/article/details/104841602

    智能推薦

    Android 的 IntentService 與 Service

    前言 之前一直用IntentService,Service,而且網上說IntentService比Service要好,好在那里?,工作中遇到關于IntentService的問題,才總結,亡羊補牢為時不晚。 背景: 做啟動頁廣告,在app 啟動時候開啟一個IntentService 去下載廣告Json,并緩存廣告圖片url,廣告時間等信息,在下次開屏時候,如果有廣告的緩存的url,就進行開屏廣告展示...

    Android 多線程之 IntentService

    簡介 1. 定義 Android 里的一個封裝類,繼承四大組件之一的 Service 2. 作用 處理異步請求 & 實現多線程 3. 使用場景 線程任務需按順序、在后臺執行。常見:離線下載。 4. 使用步驟 Tep 1:定義 IntentService的子類,需復寫onHandleIntent()方法 Tep 2:在Manifest.xml中注冊服務 Tep 3:在Activity中開啟S...

    Android學習筆記之RatingBar

                    <1>簡介 RatingBar為評分條控件,默認效果為若干個綠色的星星,如果想將其換成其他自定義圖片就要自定義它的style。 RatingBar是SeekBar 和 ProgressBar 的一...

    [Android]學習筆記之布局

    5大布局,其中前3個是常用的,第四個絕對布局已經提示deprecated RelativeLayout相對布局的3類方法 android:layout_alignParentBottom="true"是相對父容器的位置 android:layout_alignLeft="@+id/button" 對齊其它容器的邊界 android:layout_toRigh...

    android學習筆記之VideoView

    VideoView可以用來播放視頻 1.先布局 2.可以通過videoView.setVideoURI()方法來獲取網絡中的視頻,當然需要防問網絡的權限: 代碼如下,自己隨便找的視頻。 也可以通過設置路徑來獲取視頻,需要防問內存的權限 代碼如下 介紹下一些方法: 1.requestFocus();獲取焦點 2.start();播放視頻 3.setOnCompletionListener();采用匿...

    猜你喜歡

    android學習筆記之ContentProvider

    ContentProvider是android四大退件之一,對于我這種初學者是比較抽象的概念,今天我就記錄一下我的學習心得,和大家分享一下,希望對看到的各位能中點用。 ContentProvider到底是什么,它的數據到底從哪里來的,就這個問題在下也是很想知道,我們慢慢說。 首先,我們說個大致的概念,所謂ContentProvider翻譯成中文就是內容提供者。為什么geoogle會出這么個東西呢?...

    Android學習筆記之ImageView

                    ImageView類可以加載各種來源的圖片(如資源或圖片庫),需要計算圖像的尺寸,比便它可以在其他布局中使用,并提供例如縮放和著色(渲染)各種顯示選項。   <1>   屬性 屬性名 對...

    HTML中常用操作關于:頁面跳轉,空格

    1.頁面跳轉 2.空格的代替符...

    freemarker + ItextRender 根據模板生成PDF文件

    1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...

    電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!

    Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...

    精品国产乱码久久久久久蜜桃不卡