Android-圖形解鎖

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   io   ar   sp   

在項目中為了安全考慮需要加入圖形解鎖這一功能,經過搜集各方面資料後完成了開發, 需求簡單描述下(省略開啟及關閉、重設、忘記的需求)

1.應用在開啟或置於後台運行時手機鎖屏後重新喚起應用時需輸入圖形解鎖

2.應用置於後台或跳轉至第三方應用超過30s後再返回應用介面時需輸入圖形解鎖

在自訂的application中定義一個記錄時間的long型變數(hideTime),用來記錄時間,在自訂的基類activity中控製圖形解鎖(UnlockActivity)的出現,代碼如下

 1     @Override 2     protected void onStart() { 3         // TODO Auto-generated method stub 4         super.onStart(); 5         long resumeTime = System.currentTimeMillis(); 6         if (resumeTime - mApplication.hideTime > 30000) { 7             sendBroadcast(new Intent(StaticUtil.LOCK_ACTIVITY)); 8         } 9     }10 11     @Override12     protected void onResume() {13         // TODO Auto-generated method stub14         super.onResume();15         IntentFilter intentFilter = new IntentFilter();16         intentFilter.addAction(StaticUtil.FINISH_ACTIVITY);17         intentFilter.addAction(StaticUtil.LOCK_ACTIVITY);18         intentFilter.addAction(Intent.ACTION_SCREEN_OFF);19         registerReceiver(reciver, intentFilter);20 21     }22 23     @Override24     protected void onPause() {25         // TODO Auto-generated method stub26         super.onPause();27         mApplication.hideTime = System.currentTimeMillis();28     }29 30     @Override31     protected void onStop() {32         // TODO Auto-generated method stub33         super.onStop();34         mApplication.hideTime = System.currentTimeMillis();35     }36 37     @Override38     protected void onDestroy() {39         unregisterReceiver(reciver);40         super.onDestroy();41     }

開啟UnlockActivity的控制則放入自訂的廣播接收器中,action自訂為StaticUtil.LOCK_ACTIVITY,同時,開啟對鎖屏(ACTION_SCREEN_OFF)的廣播接收,經過實驗發現在

鎖屏時首先進入的是onPause 然後再接收到廣播,中間的時間差不超過1s,所以在這裡進行了如下的判斷,如果鎖屏的時間減去activity進入onPause 的時間超過1s則可以認為使用者是

先將應用置於後台再執行的鎖屏,反之則是在應用正在啟動並執行過程中鎖屏。這裡不直接在鎖屏時開啟UnlockActivity是因為在某些機器上(如mi2、努比亞z5s等)會發生只要使用者解鎖螢幕

應用的UnlockActivity總是被啟動的bug,而其他一些機型則不會(如華為榮耀3c,三星s4等)

 1 public class LotteryBroadcastReciver extends BroadcastReceiver { 2         @Override 3         public void onReceive(Context context, Intent intent) { 4             // TODO Auto-generated method stub 5             String action = intent.getAction(); 6             if (action.equals(StaticUtil.FINISH_ACTIVITY)) { 7                 finish(); 8             } else if (action.equals(StaticUtil.LOCK_ACTIVITY)) { 9                 if (mApplication.mLockPatternUtils.savedPatternExists()) {10                     Intent lockIntent = new Intent(LotteryActivity.this,11                             UnlockActivity.class);12                     lockIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP13                             | Intent.FLAG_ACTIVITY_NEW_TASK);14                     startActivity(lockIntent);15                 }16             } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {17                 long resumeTime = System.currentTimeMillis();18                 if (resumeTime - mApplication.hideTime < 1000) {19                     sendBroadcast(new Intent(StaticUtil.LOCK_ACTIVITY));20                 } else {21                     mApplication.hideTime -= 30000;22                 }23             }24         }25     }

最後,在UnlockActivity完成正確的解鎖過程後執行finish()方法並重新整理隱藏時間(hideTime)即可,而為了防止應用自身activity的開啟造成進入onPause、onStop出現鎖屏的情況,重寫了使用者點擊back鍵、onActivityResult、finish等方法,在裡面對隱藏時間(hideTime)進行了重新整理

UnlockActivity是參考了 http://blog.csdn.net/vrix/article/details/39003433 只針對業務做了微小調整。

Android-圖形解鎖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.