关于Android App App Lock上的问题

时间:2015-09-15 10:00:15

标签: java android android-activity

我在App Locker工作。我已经制作了将检查锁定的应用程序的服务,并将显示LOCK APP SCREEN,以便用户可以输入密码。我的代码工作正常,直到Lock App打开这个阶段。

我需要停止服务,因为我的服务一直在运行?当用户输入正确的凭据时,它会反复显示我的登录活动。我怎么能阻止这个问题呢? 码: 服务:

public class SeconActivity extends Activity {

    public static final String TAG = "AppLock-Abhishek";
    private String oldPasscode = null;
    protected EditText codeField1 = null;
    protected EditText codeField2 = null;
    protected EditText codeField3 = null;
    protected EditText codeField4 = null;
    protected TextView tvMessage = null;
    protected InputFilter[] filters = null;
    private String str1,str2,str3,str4;
    private ProgressDialog dialog;
    Map<String, ?> allEntries;
    SharedPreferences sharedPrefsapp;
    ArrayList<String> packagezList;
    private DbAccess dbAccess;
    private ActionBar actionBar;

    private int type=0;
    private String confirmPass=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbAccess=new DbAccess(this);
        dialog=new ProgressDialog(this);
        setContentView(R.layout.page_passcode);
        Cursor c;
        c=dbAccess.getType();
        if(c.moveToNext())
        type=c.getInt(c.getColumnIndex("type"));
        Cursor cursor;
        cursor=dbAccess.getPasssword();
        if (cursor.moveToNext()) {
            confirmPass = cursor.getString(cursor.getColumnIndex("password"));
        }
            tvMessage = (TextView) findViewById(R.id.tv_message);
        tvMessage.setText(R.string.reenter_passcode);
        //editcodeFields
        filters = new InputFilter[2];
        filters[0] = new InputFilter.LengthFilter(1);
        filters[1] = numberFilter;

        codeField1 = (EditText) findViewById(R.id.passcode_1);
        setupEditText(codeField1);

        codeField2 = (EditText) findViewById(R.id.passcode_2);
        setupEditText(codeField2);

        codeField3 = (EditText) findViewById(R.id.passcode_3);
        setupEditText(codeField3);

        codeField4 = (EditText) findViewById(R.id.passcode_4);
        setupEditText(codeField4);

        // setup the keyboard
        ((Button) findViewById(R.id.button0)).setOnClickListener(btnListener);
        ((Button) findViewById(R.id.button1)).setOnClickListener(btnListener);
        ((Button) findViewById(R.id.button2)).setOnClickListener(btnListener);
        ((Button) findViewById(R.id.button3)).setOnClickListener(btnListener);
        ((Button) findViewById(R.id.button4)).setOnClickListener(btnListener);
        ((Button) findViewById(R.id.button5)).setOnClickListener(btnListener);
        ((Button) findViewById(R.id.button6)).setOnClickListener(btnListener);
        ((Button) findViewById(R.id.button7)).setOnClickListener(btnListener);
        ((Button) findViewById(R.id.button8)).setOnClickListener(btnListener);
        ((Button) findViewById(R.id.button9)).setOnClickListener(btnListener);

        ((Button) findViewById(R.id.button_clear))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        clearFields();
                    }
                });

        ((Button) findViewById(R.id.button_erase))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onDeleteKey();
                    }
                });
    }


    private InputFilter numberFilter = new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {

            if (source.length() > 1) {
                return "";
            }

            if (source.length() == 0) // erase
            {
                return null;
            }

            try {
                int number = Integer.parseInt(source.toString());
                if ((number >= 0) && (number <= 9))
                    return String.valueOf(number);
                else
                    return "";
            } catch (NumberFormatException e) {
                return "";
            }
        }
    };

    protected  void reEnterePass()

    {
        codeField1.setText("");
        codeField2.setText("");
        codeField3.setText("");
        codeField4.setText("");
        codeField1.requestFocus();


        // set the value and move the focus
    }

    protected void onPasscodeError() {
        Toast toast = Toast.makeText(this, getString(R.string.passcode_wrong),
                Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 30);
        toast.show();

        Thread thread = new Thread() {
            public void run() {
                Animation animation = AnimationUtils.loadAnimation(
                        SeconActivity.this, R.anim.shake);
                findViewById(R.id.ll_applock).startAnimation(animation);
                codeField1.setText("");
                codeField2.setText("");
                codeField3.setText("");
                codeField4.setText("");
                codeField1.requestFocus();
            }
        };
        runOnUiThread(thread);
    }
    protected void setupEditText(EditText editText) {
        editText.setInputType(InputType.TYPE_NULL);
        editText.setFilters(filters);
        editText.setOnTouchListener(touchListener);
        editText.setTransformationMethod(PasswordTransformationMethod
                .getInstance());
    }


    private View.OnTouchListener touchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.performClick();
            clearFields();
            return false;
        }
    };

    private void clearFields() {
        codeField1.setText("");
        codeField2.setText("");
        codeField3.setText("");
        codeField4.setText("");
        codeField1.postDelayed(new Runnable() {

            @Override
            public void run() {
                codeField1.requestFocus();
            }
        }, 200);
    }

    private View.OnClickListener btnListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int currentValue = -1;
            int id = view.getId();
            if (id == R.id.button0) {
                currentValue = 0;
            } else if (id == R.id.button1) {
                currentValue = 1;
            } else if (id == R.id.button2) {
                currentValue = 2;
            } else if (id == R.id.button3) {
                currentValue = 3;
            } else if (id == R.id.button4) {
                currentValue = 4;
            } else if (id == R.id.button5) {
                currentValue = 5;
            } else if (id == R.id.button6) {
                currentValue = 6;
            } else if (id == R.id.button7) {
                currentValue = 7;
            } else if (id == R.id.button8) {
                currentValue = 8;
            } else if (id == R.id.button9) {
                currentValue = 9;
            } else {
            }

            // set the value and move the focus
            String currentValueString = String.valueOf(currentValue);
            if (codeField1.isFocused()) {
                codeField1.setText(currentValueString);
                str1=currentValueString;
                codeField2.requestFocus();
                codeField2.setText("");

            } else if (codeField2.isFocused()) {
                codeField2.setText(currentValueString);
                str2=currentValueString;
                codeField3.requestFocus();
                codeField3.setText("");
            } else if (codeField3.isFocused()) {
                codeField3.setText(currentValueString);
                str3=currentValueString;
                codeField4.requestFocus();
                codeField4.setText("");

            } else if (codeField4.isFocused()) {
                codeField4.setText(currentValueString);
                str4=currentValueString;
            }
            if (codeField4.getText().toString().length() > 0
                    && codeField3.getText().toString().length() > 0
                    && codeField2.getText().toString().length() > 0
                    && codeField1.getText().toString().length() > 0) {
                Log.e(TAG, str1 + str2 + str3 + str4);
                String passCode=(str1+str2+str3+str4).trim();
                switch (type){

                    case 1:
                            if (passCode.equals(confirmPass)){
                                startActivity(new Intent(getApplicationContext(),LockScreenActivity.class));
                            }else
                            {
                                onPasscodeError();
                            }


                        break;
                    case 2:

                        if(passCode.equalsIgnoreCase(confirmPass)){

                            finish();

                        }else
                        {
                            Intent startHomescreen=new Intent(Intent.ACTION_MAIN);
                            startHomescreen.addCategory(Intent.CATEGORY_HOME);
                            startHomescreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(startHomescreen);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    };

    private void onDeleteKey() {
        if (codeField1.isFocused()) {

        } else if (codeField2.isFocused()) {
            codeField1.requestFocus();
            codeField1.setText("");
        } else if (codeField3.isFocused()) {
            codeField2.requestFocus();
            codeField2.setText("");
        } else if (codeField4.isFocused()) {
            codeField3.requestFocus();
            codeField3.setText("");
        }
    }
}//end of class

LockScreenActivity:

{{1}}

1 个答案:

答案 0 :(得分:1)

首先,我想告诉你,你不应该依赖Timer来重复运行你的代码,因为计时器在一段时间后被系统破坏了,我希望你能意识到这个bug。对于您的问题,您应该创建一个哈希映射,以通过验证密码来了解用户已经访问过前景活动的天气。