乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Android靜態(tài)注冊(cè)無(wú)法接受系統(tǒng)廣播問(wèn)題

       cathy001 2016-09-12

      概要

           工作中遇到一個(gè)問(wèn)題:監(jiān)控應(yīng)用的后臺(tái)Service需要一直運(yùn)行不退出。為了選用合適的方法,搜索了網(wǎng)上常用保持進(jìn)程或者Service不被殺死的方法,監(jiān)聽(tīng)開(kāi)機(jī)廣播、解鎖屏廣播等有規(guī)律廣播是很好的選擇。當(dāng)應(yīng)用運(yùn)行在android2.x版本上時(shí)一切正常,但是在android4.x版本上發(fā)現(xiàn)應(yīng)用被用戶在系統(tǒng)Settings中強(qiáng)制force stop后,無(wú)法監(jiān)聽(tīng)到此類廣播,導(dǎo)致監(jiān)控應(yīng)用不能正常運(yùn)行。
           調(diào)查發(fā)現(xiàn),這是android系統(tǒng)在3.1版本以后為了加強(qiáng)系統(tǒng)安全性和優(yōu)化性能對(duì)系統(tǒng)廣播進(jìn)行了限制。

      Service運(yùn)行不退出的常用方法

          總結(jié)常用保證進(jìn)程或者Service后臺(tái)運(yùn)行不退出的方法如下:
           1. 應(yīng)用監(jiān)控開(kāi)機(jī)廣播boot_completed、解鎖屏廣播user_present、網(wǎng)絡(luò)連接狀態(tài)改變廣播CONNECTIVITY_CHANGE等有規(guī)律的系統(tǒng)廣播
               --android3.1以后,首次安裝未啟動(dòng)或者用戶強(qiáng)制force stop后,應(yīng)用無(wú)法監(jiān)聽(tīng)到
               --應(yīng)用啟動(dòng)后,可以接收到上述廣播,但是會(huì)導(dǎo)致手機(jī)啟動(dòng)變慢,耗電明顯
           2. 放到系統(tǒng)應(yīng)用system/app/或者system/priv-app/目錄下永遠(yuǎn)可以監(jiān)聽(tīng)1中的廣播
               --僅適用于預(yù)裝應(yīng)用,或者手機(jī)已經(jīng)root可以操作系統(tǒng)目錄
           3. 設(shè)置應(yīng)用的application屬性persistent為true,保證應(yīng)用開(kāi)機(jī)后一直后臺(tái)運(yùn)行
               --僅適用于系統(tǒng)應(yīng)用,第三方應(yīng)用無(wú)效果
           4. 開(kāi)啟兩個(gè)獨(dú)立應(yīng)用進(jìn)程,相互監(jiān)控,一方被kill,由對(duì)方立即啟動(dòng)
               --一個(gè)應(yīng)用內(nèi)開(kāi)啟兩個(gè)進(jìn)程,android3.1以后被用戶force stop后依然無(wú)法監(jiān)聽(tīng)到1中的廣播
               --開(kāi)啟兩個(gè)獨(dú)立應(yīng)用進(jìn)程,首次啟動(dòng)后可以實(shí)現(xiàn)相互監(jiān)控。但是用戶體驗(yàn)不好,需要安裝兩個(gè)apk。
           5. Service中調(diào)用AlarmManager的定時(shí)器功能,周期性檢查啟動(dòng)Service
               --由于pending Intent由系統(tǒng)AlarmManager發(fā)送,android3.1以后應(yīng)用被force stop后無(wú)法收到
               --定期發(fā)送造成應(yīng)用耗電明顯
           6. Service內(nèi)部處理,保證不退出
               --在onStartCommand中設(shè)置start_sticky的返回值,可以保證Service因內(nèi)存不足被kill、內(nèi)存可用時(shí)被重啟
               --在onDestory時(shí)重啟該Service
               --提高Service優(yōu)先級(jí):調(diào)用startForeground設(shè)置service為前臺(tái)
               --應(yīng)用被force stop后這些方法無(wú)效
          不過(guò)在android3.1以后的版本上,對(duì)于第三方APK,上述幾種方法僅能盡量保證進(jìn)程或者service不退出,但不是100%保證。

      Android對(duì)于系統(tǒng)廣播的控制

            從Android3.1開(kāi)始,android對(duì)于系統(tǒng)發(fā)送的廣播進(jìn)行了限制,原文如下:

      Launch controls on stoppedapplications
      
      Starting from Android 3.1, the system's package manager keeps track ofapplications that are in a stopped state and provides a means 
      of controllingtheir launch from background processes and other applications.
      
      Note that an application's stopped state is not the same as an Activity'sstopped state. The system manages those two stopped states 
      separately.
      
      The platform defines two new intent flags that let a sender specifywhether the Intent should be allowed to activate components in
       stoppedapplication.
      FLAG_INCLUDE_STOPPED_PACKAGES —Include intent filters of stopped applications in the list of potential targetsto resolve against.
      FLAG_EXCLUDE_STOPPED_PACKAGES —Exclude intent filters of stopped applications from the list of potentialtargets.
      
      When neither or both of these flags is defined in an intent, the defaultbehavior is to include filters of stopped applications in 
      the list ofpotential targets.
      
      Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGESto all broadcastintents. It does this to prevent broadcasts from background
      services frominadvertently or unnecessarily launching components of stoppped applications.A background service or application can 
      override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcastintents that should be allowed to activate stopped
       applications.
      
      Applications are in a stopped state when they are first installed but are notyet launched and when they are manually stopped by the
       user (in ManageApplications).

      Android官方API說(shuō)明:http://developer./about/versions/android-3.1.html#launchcontrols

            對(duì)于上述描述總結(jié)如下:
            1.在Android3.1以后版本添加了標(biāo)志FLAG_INCLUDE_STOPPED_PACKAGES和FLAG_EXCLUDE_STOPPED_PACKAGES,用于區(qū)分發(fā)送廣播時(shí)是否啟動(dòng)激活那些未啟動(dòng)過(guò)或者被用戶force stop的應(yīng)用組件。當(dāng)兩個(gè)Flag都不設(shè)置或都設(shè)置的時(shí)候,默認(rèn)操作是FLAG_INCLUDE_STOPPED_PACKAGES。
            2.系統(tǒng)向所有的Intent的廣播添加了FL??AG_EXCLUDE_STOPPED_PACKAGES標(biāo)志。它這樣做是為了防止廣播無(wú)意中的或不必要地開(kāi)啟組件的stoppped應(yīng)用程序的后臺(tái)服務(wù)。這樣可以優(yōu)化系統(tǒng)性能,提高安全性,不過(guò)對(duì)于監(jiān)控類軟件是一個(gè)沉重的打擊。
            3.用戶給自定義的廣播Intent添加FLAG_INCLUDE_STOPPED_PACKAGES,用于啟動(dòng)stop狀態(tài)的應(yīng)用組件。但是系統(tǒng)自帶的廣播intent,我們無(wú)能為力。
            綜上,在android3.1以后,系統(tǒng)加強(qiáng)了廣播的限制,對(duì)于進(jìn)程或者service長(zhǎng)時(shí)間運(yùn)行不退出沒(méi)有有效的解決方案。總體來(lái)看,啟動(dòng)兩個(gè)獨(dú)立應(yīng)用進(jìn)程是一個(gè)不錯(cuò)的選擇,一旦用戶啟動(dòng)應(yīng)用后,即使點(diǎn)擊force stop,也可以由另一個(gè)應(yīng)用啟動(dòng)。概要

           工作中遇到一個(gè)問(wèn)題:監(jiān)控應(yīng)用的后臺(tái)Service需要一直運(yùn)行不退出。為了選用合適的方法,搜索了網(wǎng)上常用保持進(jìn)程或者Service不被殺死的方法,監(jiān)聽(tīng)開(kāi)機(jī)廣播、解鎖屏廣播等有規(guī)律廣播是很好的選擇。當(dāng)應(yīng)用運(yùn)行在android2.x版本上時(shí)一切正常,但是在android4.x版本上發(fā)現(xiàn)應(yīng)用被用戶在系統(tǒng)Settings中強(qiáng)制force stop后,無(wú)法監(jiān)聽(tīng)到此類廣播,導(dǎo)致監(jiān)控應(yīng)用不能正常運(yùn)行。
           調(diào)查發(fā)現(xiàn),這是android系統(tǒng)在3.1版本以后為了加強(qiáng)系統(tǒng)安全性和優(yōu)化性能對(duì)系統(tǒng)廣播進(jìn)行了限制。

      Service運(yùn)行不退出的常用方法

          總結(jié)常用保證進(jìn)程或者Service后臺(tái)運(yùn)行不退出的方法如下:
           1. 應(yīng)用監(jiān)控開(kāi)機(jī)廣播boot_completed、解鎖屏廣播user_present、網(wǎng)絡(luò)連接狀態(tài)改變廣播CONNECTIVITY_CHANGE等有規(guī)律的系統(tǒng)廣播
               --android3.1以后,首次安裝未啟動(dòng)或者用戶強(qiáng)制force stop后,應(yīng)用無(wú)法監(jiān)聽(tīng)到
               --應(yīng)用啟動(dòng)后,可以接收到上述廣播,但是會(huì)導(dǎo)致手機(jī)啟動(dòng)變慢,耗電明顯
           2. 放到系統(tǒng)應(yīng)用system/app/或者system/priv-app/目錄下永遠(yuǎn)可以監(jiān)聽(tīng)1中的廣播
               --僅適用于預(yù)裝應(yīng)用,或者手機(jī)已經(jīng)root可以操作系統(tǒng)目錄
           3. 設(shè)置應(yīng)用的application屬性persistent為true,保證應(yīng)用開(kāi)機(jī)后一直后臺(tái)運(yùn)行
               --僅適用于系統(tǒng)應(yīng)用,第三方應(yīng)用無(wú)效果
           4. 開(kāi)啟兩個(gè)獨(dú)立應(yīng)用進(jìn)程,相互監(jiān)控,一方被kill,由對(duì)方立即啟動(dòng)
               --一個(gè)應(yīng)用內(nèi)開(kāi)啟兩個(gè)進(jìn)程,android3.1以后被用戶force stop后依然無(wú)法監(jiān)聽(tīng)到1中的廣播
               --開(kāi)啟兩個(gè)獨(dú)立應(yīng)用進(jìn)程,首次啟動(dòng)后可以實(shí)現(xiàn)相互監(jiān)控。但是用戶體驗(yàn)不好,需要安裝兩個(gè)apk。
           5. Service中調(diào)用AlarmManager的定時(shí)器功能,周期性檢查啟動(dòng)Service
               --由于pending Intent由系統(tǒng)AlarmManager發(fā)送,android3.1以后應(yīng)用被force stop后無(wú)法收到
               --定期發(fā)送造成應(yīng)用耗電明顯
           6. Service內(nèi)部處理,保證不退出
               --在onStartCommand中設(shè)置start_sticky的返回值,可以保證Service因內(nèi)存不足被kill、內(nèi)存可用時(shí)被重啟
               --在onDestory時(shí)重啟該Service
               --提高Service優(yōu)先級(jí):調(diào)用startForeground設(shè)置service為前臺(tái)
               --應(yīng)用被force stop后這些方法無(wú)效
          不過(guò)在android3.1以后的版本上,對(duì)于第三方APK,上述幾種方法僅能盡量保證進(jìn)程或者service不退出,但不是100%保證。

      Android對(duì)于系統(tǒng)廣播的控制

            從Android3.1開(kāi)始,android對(duì)于系統(tǒng)發(fā)送的廣播進(jìn)行了限制,原文如下:

      Launch controls on stoppedapplications
      
      Starting from Android 3.1, the system's package manager keeps track ofapplications that are in a stopped state and provides a means 
      of controllingtheir launch from background processes and other applications.
      
      Note that an application's stopped state is not the same as an Activity'sstopped state. The system manages those two stopped states 
      separately.
      
      The platform defines two new intent flags that let a sender specifywhether the Intent should be allowed to activate components in
       stoppedapplication.
      FLAG_INCLUDE_STOPPED_PACKAGES —Include intent filters of stopped applications in the list of potential targetsto resolve against.
      FLAG_EXCLUDE_STOPPED_PACKAGES —Exclude intent filters of stopped applications from the list of potentialtargets.
      
      When neither or both of these flags is defined in an intent, the defaultbehavior is to include filters of stopped applications in 
      the list ofpotential targets.
      
      Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGESto all broadcastintents. It does this to prevent broadcasts from background
      services frominadvertently or unnecessarily launching components of stoppped applications.A background service or application can 
      override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcastintents that should be allowed to activate stopped
       applications.
      
      Applications are in a stopped state when they are first installed but are notyet launched and when they are manually stopped by the
       user (in ManageApplications).

      Android官方API說(shuō)明:http://developer./about/versions/android-3.1.html#launchcontrols

            對(duì)于上述描述總結(jié)如下:
            1.在Android3.1以后版本添加了標(biāo)志FLAG_INCLUDE_STOPPED_PACKAGES和FLAG_EXCLUDE_STOPPED_PACKAGES,用于區(qū)分發(fā)送廣播時(shí)是否啟動(dòng)激活那些未啟動(dòng)過(guò)或者被用戶force stop的應(yīng)用組件。當(dāng)兩個(gè)Flag都不設(shè)置或都設(shè)置的時(shí)候,默認(rèn)操作是FLAG_INCLUDE_STOPPED_PACKAGES。
            2.系統(tǒng)向所有的Intent的廣播添加了FL??AG_EXCLUDE_STOPPED_PACKAGES標(biāo)志。它這樣做是為了防止廣播無(wú)意中的或不必要地開(kāi)啟組件的stoppped應(yīng)用程序的后臺(tái)服務(wù)。這樣可以優(yōu)化系統(tǒng)性能,提高安全性,不過(guò)對(duì)于監(jiān)控類軟件是一個(gè)沉重的打擊。
            3.用戶給自定義的廣播Intent添加FLAG_INCLUDE_STOPPED_PACKAGES,用于啟動(dòng)stop狀態(tài)的應(yīng)用組件。但是系統(tǒng)自帶的廣播intent,我們無(wú)能為力。
            綜上,在android3.1以后,系統(tǒng)加強(qiáng)了廣播的限制,對(duì)于進(jìn)程或者service長(zhǎng)時(shí)間運(yùn)行不退出沒(méi)有有效的解決方案。總體來(lái)看,啟動(dòng)兩個(gè)獨(dú)立應(yīng)用進(jìn)程是一個(gè)不錯(cuò)的選擇,一旦用戶啟動(dòng)應(yīng)用后,即使點(diǎn)擊force stop,也可以由另一個(gè)應(yīng)用啟動(dòng)。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多