android 的 Keyguard 可以用來呼叫螢幕解鎖來驗證,
運用在有做自動登入時,避免使用者手機被偷,可以在app的重要功能再次要求解鎖
以下這個範例,
按下Verify按鈕時,會依照你的手機設定的解鎖方式開啟驗證(PIN、Password、Pattern、Fingerprint)



要使用Keyguard,API Level 21 以上。
先檢查手機是否有開啟螢幕鎖定。
在Activity 叫起驗證頁,可以客制Title、Description
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void showAuthenticationScreen() { | |
if (keyguardManager.isKeyguardSecure()) { | |
// Custom title description | |
Intent intent = keyguardManager.createConfirmDeviceCredentialIntent("This is a custom title", "This is a custom description"); | |
if (intent != null) { | |
startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS); | |
}else{ | |
this.result.setText("Secure lock screen hasn't set up"); | |
} | |
}else{ | |
this.result.setText("Secure lock screen hasn't set up"); | |
} | |
} |
接收解鎖結果:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) { | |
if (resultCode == RESULT_OK) { | |
this.result.setText("Success"); | |
} else { | |
this.result.setText("Cancel"); | |
} | |
} | |
} |
完整程式:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 1; | |
private KeyguardManager keyguardManager; | |
private TextView result; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
this.result = (TextView)findViewById(R.id.result); | |
keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); | |
} | |
private void showAuthenticationScreen() { | |
if (keyguardManager.isKeyguardSecure()) { | |
// Custom title description | |
Intent intent = keyguardManager.createConfirmDeviceCredentialIntent("This is a custom title", "This is a custom description"); | |
if (intent != null) { | |
startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS); | |
}else{ | |
this.result.setText("Secure lock screen hasn't set up"); | |
} | |
}else{ | |
this.result.setText("Secure lock screen hasn't set up"); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) { | |
if (resultCode == RESULT_OK) { | |
this.result.setText("Success"); | |
} else { | |
this.result.setText("Cancel"); | |
} | |
} | |
} | |
public void verify(View view){ | |
this.showAuthenticationScreen(); | |
} | |
} |
參考:
沒有留言:
張貼留言