您的位置:首页 > 其它

实现安卓应用程序锁功能

2015-08-26 17:17 92 查看
一、实现原理:

1.监听屏幕关闭广播。

2.监听home键广播。

二、实现代码

1.创建一个自定义的Application,在Application中注册监听的广播:

public class AssistantApplication extends Application {
public boolean isLocked = false;
LockScreenReceiver receiver ;
IntentFilter filter ;
@Override
public void onCreate() {
super.onCreate();
receiver = new LockScreenReceiver();

         filter = new IntentFilter();

        filter.addAction(Intent.ACTION_SCREEN_OFF);

        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);

        this.registerReceiver(receiver, filter);
}
@Override
public void onTerminate() {
super.onTerminate();
try {
this.unregisterReceiver(receiver);
} catch (Exception e) {
e.printStackTrace();
}
}

class LockScreenReceiver extends BroadcastReceiver {
String SYSTEM_REASON = "reason";  
String SYSTEM_HOME_KEY = "homekey";
@Override
public void onReceive(Context context, Intent intent) {
/* 在这里处理广播 */
if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
isLocked  = true;
}else if(Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())){
String reason = intent.getStringExtra(SYSTEM_REASON);  

                if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {  

                     //表示按了home键,程序到了后台  

                isLocked  = true;

                }
}
}
}

2.自定一个全局的BaseActivity,在onResume()方法中判断是否要输入密码打开程序,所有的业务界面都继承这个类:

@Override
protected void onResume() {
super.onResume();
myApplaction = (AssistantApplication) getApplication();
if (Constants.USE_APP_LOCK) {//启用程序锁
if (myApplaction.isLocked) {// 判断是否需要跳转到密码界面
Intent intent = new Intent(this, LockPassActivity.class);
startActivity(intent);
}
}
}

3.实现一个输入密码的界面,继承Activity:

public class LockPassActivity extends Activity {
LinearLayout ll_pass;
AssistantApplication myApplaction;
Button btn_login;
EditText et_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock_system);
myApplaction = (AssistantApplication) getApplication();
ll_pass = (LinearLayout) findViewById(R.id.ll_pass);

createPassEditView(ll_pass);
}

public void createPassEditView(LinearLayout ll_pass) {
String[] ps = Constants.USER_LOGIN_PASS.split("");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = 100;

for (int i = 0; i < ps.length - 1; i++) {
EditText et = new EditText(this);
et.setId(i + 1);
// et.setLayoutParams(params);
// 密码显示
et.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
et.addTextChangedListener(new MyTextWatcher(ll_pass, i));
ll_pass.addView(et);
}
}

class MyTextWatcher implements TextWatcher {
LinearLayout ll_pass;
int index;

public MyTextWatcher(LinearLayout ll_pass, int index) {
this.ll_pass = ll_pass;
this.index = index;
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() == 1) {
if (index < ll_pass.getChildCount() - 1) {
EditText et1 = (EditText) ll_pass.getChildAt(index);
EditText et2 = (EditText) ll_pass.getChildAt(index + 1);
if (et1.isFocused()) {
et1.clearFocus();
if (Character.isDigit((et1.getText().toString())
.charAt(0))) {// 判断前一个输入的是否是数字
et2.setRawInputType(InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.restartInput(et2);
}
et2.requestFocus();
}
} else if (index == ll_pass.getChildCount() - 1) {// 是最后一个
EditText et1 = (EditText) ll_pass.getChildAt(index);
if (et1.isFocused()) {
et1.clearFocus();
String password = "";
for (int i = 0; i < ll_pass.getChildCount(); i++) {
EditText et = (EditText) ll_pass.getChildAt(i);
password += et.getText().toString();
}
if (password != null
&& password.equals(Constants.USER_LOGIN_PASS)) {
Toast.makeText(LockPassActivity.this, "密码正确!",
Toast.LENGTH_SHORT).show();
myApplaction.isLocked = false;
LockPassActivity.this.finish();
} else {
Toast.makeText(LockPassActivity.this, "密码错误!",
Toast.LENGTH_SHORT).show();
for (int i = 0; i < ll_pass.getChildCount(); i++) {
((EditText) ll_pass.getChildAt(i)).setText("");
}
}
}
}
}
}

}

}

以上代码就是实现程序锁的过程。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: