2013-11-21 120 views
4

我目前正在構想應用程序的功能,在此我想要一種通用方法/方法來檢測應用程序本身是從「 外部'的應用程序。檢測應用程序是否從應用程序的「外部」啓動/恢復

」,在這種情況下,是指:

  • 應用啓動/由啓動器圖標
  • 應用開始/重新開始通過從導航欄按下「應用按鈕」恢復從通知
  • 應用啓動/從「別處」
恢復/鍵(如在Nexus 7)
  • 應用啓動/重新開始210

    使用情況下此功能是:

    • 程序裏有個「多用戶能力」,允許用戶(一個或多個)創建爲他/她的數據
    • 一個或多個配置文件
    • 單個簡檔可以是銷/密碼保護「隱藏」從應用程序的其他(多個)用戶,或從安裝該應用的設備的其他(多個)用戶「隱藏」的數據的數據
      • 如果配置文件設置了密碼,則應用程序啓動/恢復時,應用程序會向當前用戶顯示某種鎖定屏幕
        • 如果輸入正確,應用程序將正常啓動顯示最後選擇輪廓
        • 的數據如果輸​​入錯誤,應用程序將與所有
    • 一個「中立」的個人資料或沒有配置文件啓動

    我搜索的想法網站,發現只有計算器相關帖子:

    從我讀過的和學到到目前爲止,該解決方案似乎比我想過要更加複雜,沒有現成的解決方案爲這個。

    目前,我正在思考一個時間標誌基於的方法來實現此功能:

    • 設定一個時間標誌作爲一個受影響的活動的成員變量
    • onCreate(Bundle savedInstanceState) - >標誌>如果密碼被設置 - - 檢查savedInstanceState Budle用於數據
      • 此檢測到活動開始前設置爲「空」>顯示在鎖定屏幕
    • onSaveInstanceState(Bundle) - >設定的時間標誌,以
    • 如果onCreate(Bundle savedInstanceState)恢復「當前時間」,savedInstanceState將包含一個時間標誌
      • 計算當前時間和時間的應用程序被暫停之間的差異最後
      • 如果此差異高於某個閾值,例如30分鐘 - >如果密碼設置 - >顯示在鎖屏

    也許你們當中有些已經實現的東西確實有一些輸入此事/方法similiar或。 我很樂意聽到你的想法。

    乾杯

  • +0

    你的「內部」情況是什麼樣的? –

    +0

    目前,唯一的'內部'情況下(當需要鎖定屏幕時)我看到的是一個配置文件,例如配置文件A被「切換」/「選擇」到另一個配置文件,例如,配置文件B,由用戶。 – darksaga

    +1

    當活動恢復時,可以將「外部」情況設置爲默認情況,但如果另一活動正在啓動,它會傳入套件中的Extra以告知類不執行默認鎖定。 –

    回答

    1

    這是一個較老的問題,但仍然相關。有一個簡單的解決方案使用ActivityLifeCycleCallbacks。這個答案來自Micahel Bradshaw的blogpost。他解釋了這個概念

    關鍵在於瞭解活動如何相互協調。當活動A和B之間進行切換,它們的方法被稱爲順序是:

    A.onPause();

    B.onCreate();

    B.onStart();

    B.onResume();(活動B現已用戶聚焦)

    A.onStop();(如果活動A在屏幕上不再可見)

    解決方案:您創建一個實現Application.ActivityLifecycleCallbacks接口的類,並保持恢復和停止活動的次數。

    public class AppLifecycleHelper implements Application.ActivityLifecycleCallbacks { 
    
    // I use two separate variables here. You can, of course, just use one and 
    // increment/decrement it instead of using two and incrementing both. 
    private static int resumed; 
    private static int stopped; 
    
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) { 
        } 
    
        public void onActivityDestroyed(Activity activity) { 
        } 
    
        public void onActivityResumed(Activity activity) { 
         ++resumed; 
        } 
    
        public void onActivityPaused(Activity activity) { 
        } 
    
        public void onActivitySaveInstanceState(Activity activity, Bundle  outState) { 
        } 
    
        public void onActivityStarted(Activity activity) { 
         if (!isApplicationInForeground()){ 
          // resumed and stopped both are 0, 
          // that means it is the first activity to come on display 
          // i.e. App is launched from outside the app 
         } 
        } 
    
        public void onActivityStopped(Activity activity) { 
         ++stopped; 
         if (!isApplicationInForeground()){ 
          // resumed and stopped both are 0 
          // That means there is NO Activity in resumed state when this activity stopped 
          // i.e. User came out of the App, perform all Application wide persistence tasks over here 
         } 
        } 
    
        /** 
        * Checks whether App is visible to the user or not 
        * @return true if visible and false otherwise 
        */ 
        public static boolean isApplicationInForeground() { 
         return resumed > stopped; 
        } 
    
    } 
    

    然後在您的應用程序的onCreate()註冊這個類活動回調這樣

    registerActivityLifecycleCallbacks(new AppLifecycleHelper());

    ,這就是它!無需爲每個活動添加任何代碼。AppLifecycleHelper中只包含幾行代碼。

    相關問題