安卓日记——教你做简单的应用锁

我百度了一下关于应用锁的内容,大部分都是用service来实现,作为一个入门的人来说,用service来做一个的确有点难度。现在有一种比较简单的方法,总得来说就是用一个值来记录这次返回或者打开Activity是否是由按键打开,是的话则不弹出锁屏,不是的话则弹出锁屏,我们这次用sharepreference来储存这个值

一、我们先新建一个BaseActivity,因为这些Activity都是在onResume判断是否为按键打开或返回

public class BaseActivity extends AppCompatActivity {
    public SharedPreferences sharedPreferences;
    public SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPreferences=getSharedPreferences("sp",MODE_PRIVATE);//sp为sharepreference名字
        editor=sharedPreferences.edit();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //如果lock值是false是真的就弹出锁屏,假的话就设为真
        if (sharedPreferences.getBoolean("lock",false)){
            startActivity(new Intent(BaseActivity.this,LockActivity.class));
            editor.putBoolean("lock",false);
            editor.commit();
        }else {
            editor.putBoolean("lock",true);
            editor.commit();
        }

    }

    //按返回键时将lock设为假
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            editor.putBoolean("lock",false);
            editor.commit();
        }
        return super.onKeyDown(keyCode, event);
    }
}

二、新建Activity继承自BaseActivity

package com.jkgeekjack.newlock;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends BaseActivity {
    private Button button;
    private Button button1;
    private Button button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button= (Button) findViewById(R.id.button);
        button1= (Button) findViewById(R.id.button2);
        button2= (Button) findViewById(R.id.button3);
        //点击跳转时将lock值设为假
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,Main2Activity.class));
                editor.putBoolean("lock",false);
                editor.commit();
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,Main3Activity.class));
                editor.putBoolean("lock",false);
                editor.commit();
            }
        });
        //点击finish时将lock值设为假
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor.putBoolean("lock",false);
                editor.commit();
                finish();
            }
        });
    }

}

其他Activity基本和这个是一样的,都是继承自BaseActivity,有按键点击跳转或者返回时将lock值设为假

三、新建锁界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textSize="30dip"
        android:layout_marginTop="40dp"
        android:text="请输入密码"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/border">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/ivCircle1"
            android:src="@drawable/circle"/>
        <TextView
            android:layout_width="1dp"
            android:layout_height="40dp"
            android:background="@android:color/darker_gray"
            android:gravity="center"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/ivCircle2"
            android:src="@drawable/circle"/>
        <TextView
            android:layout_width="1dp"
            android:layout_height="40dp"
            android:background="@android:color/darker_gray"
            android:gravity="center"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/ivCircle3"
            android:src="@drawable/circle"/>
        <TextView
            android:layout_width="1dp"
            android:layout_height="40dp"
            android:background="@android:color/darker_gray"
            android:gravity="center"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/ivCircle4"
            android:src="@drawable/circle"/>
        <TextView
            android:layout_width="1dp"
            android:layout_height="40dp"
            android:background="@android:color/darker_gray"
            android:gravity="center"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="50dp"
        android:orientation="vertical"
        android:gravity="bottom">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv1"
                android:textSize="20dp"
                android:gravity="center"
                android:text="1"
                android:layout_weight="1"
                android:padding="10dp"/>
            <TextView
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@android:color/darker_gray"
                android:gravity="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv2"
                android:textSize="20dp"
                android:gravity="center"
                android:text="2"
                android:layout_weight="1"
                android:padding="10dp"/>
            <TextView
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@android:color/darker_gray"
                android:gravity="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv3"
                android:textSize="20dp"
                android:gravity="center"
                android:text="3"
                android:layout_weight="1"
                android:padding="10dp"/>
        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv4"
                android:textSize="20dp"
                android:gravity="center"
                android:text="4"
                android:layout_weight="1"
                android:padding="10dp"/>
            <TextView
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@android:color/darker_gray"
                android:gravity="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv5"
                android:textSize="20dp"
                android:gravity="center"
                android:text="5"
                android:layout_weight="1"
                android:padding="10dp"/>
            <TextView
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@android:color/darker_gray"
                android:gravity="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv6"
                android:textSize="20dp"
                android:gravity="center"
                android:text="6"
                android:layout_weight="1"
                android:padding="10dp"/>
        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv7"
                android:textSize="20dp"
                android:gravity="center"
                android:text="7"
                android:layout_weight="1"
                android:padding="10dp"/>
            <TextView
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@android:color/darker_gray"
                android:gravity="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv8"
                android:textSize="20dp"
                android:gravity="center"
                android:text="8"
                android:layout_weight="1"
                android:padding="10dp"/>
            <TextView
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@android:color/darker_gray"
                android:gravity="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv9"
                android:textSize="20dp"
                android:gravity="center"
                android:text="9"
                android:layout_weight="1"
                android:padding="10dp"/>
        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv10"
                android:textSize="20dp"
                android:gravity="center"
                android:text="        "
                android:background="@android:color/darker_gray"
                android:layout_weight="1"
                android:padding="10dp"/>
            <TextView
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@android:color/darker_gray"
                android:gravity="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tv0"
                android:textSize="20dp"
                android:gravity="center"
                android:text="   0   "
                android:layout_weight="1"
                android:padding="10dp"/>
            <TextView
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@android:color/darker_gray"
                android:gravity="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/tvCancle"
                android:textSize="20dp"
                android:gravity="center"
                android:text="回删"
                android:background="@android:color/darker_gray"
                android:layout_weight="1"
                android:padding="10dp"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

锁界面的逻辑代码

package com.jkgeekjack.newlock;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class LockActivity extends Activity implements View.OnClickListener {
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private TextView tv5;
    private TextView tv6;
    private TextView tv7;
    private TextView tv8;
    private TextView tv9;
    private TextView tv0;
    private TextView tvCancle;
    private ImageView ivCircle1;
    private ImageView ivCircle2;
    private ImageView ivCircle3;
    private ImageView ivCircle4;
    private String insertPwd;
    private List<ImageView> circles=new ArrayList<ImageView>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock);
        initView();
        initListener();
    }
    private void initListener() {
        tv0.setOnClickListener(this);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv5.setOnClickListener(this);
        tv6.setOnClickListener(this);
        tv7.setOnClickListener(this);
        tv8.setOnClickListener(this);
        tv9.setOnClickListener(this);
        tvCancle.setOnClickListener(this);
    }
    private void initView() {
        tv0= (TextView) findViewById(R.id.tv0);
        tv1= (TextView) findViewById(R.id.tv1);
        tv2= (TextView) findViewById(R.id.tv2);
        tv3= (TextView) findViewById(R.id.tv3);
        tv4= (TextView) findViewById(R.id.tv4);
        tv5= (TextView) findViewById(R.id.tv5);
        tv6= (TextView) findViewById(R.id.tv6);
        tv7= (TextView) findViewById(R.id.tv7);
        tv8= (TextView) findViewById(R.id.tv8);
        tv9= (TextView) findViewById(R.id.tv9);
        tv0= (TextView) findViewById(R.id.tv0);
        tvCancle= (TextView) findViewById(R.id.tvCancle);
        ivCircle1= (ImageView) findViewById(R.id.ivCircle1);
        ivCircle2= (ImageView) findViewById(R.id.ivCircle2);
        ivCircle3= (ImageView) findViewById(R.id.ivCircle3);
        ivCircle4= (ImageView) findViewById(R.id.ivCircle4);
        circles.add(ivCircle1);
        circles.add(ivCircle2);
        circles.add(ivCircle3);
        circles.add(ivCircle4);
        clearCircle();
        insertPwd="";
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv0:addToPwd(0);break;
            case R.id.tv1:addToPwd(1);break;
            case R.id.tv2:addToPwd(2);break;
            case R.id.tv3:addToPwd(3);break;
            case R.id.tv4:addToPwd(4);break;
            case R.id.tv5:addToPwd(5);break;
            case R.id.tv6:addToPwd(6);break;
            case R.id.tv7:addToPwd(7);break;
            case R.id.tv8:addToPwd(8);break;
            case R.id.tv9:addToPwd(9);break;
            case R.id.tvCancle:
                if (insertPwd.length()>0){
                    insertPwd=insertPwd.substring(0,insertPwd.length()-1);
                }
                updateUi();
                break;
        }
    }
    //每输入一位刷新一次UI
    public void addToPwd(int num){
        insertPwd+=num;
        updateUi();
        //满4位确认是否为正确密码
        if (insertPwd.length()==4){
            comfirmPwd();
        }
    }
    private void comfirmPwd() {
        String pwd="1234";
        if (pwd.equals(insertPwd)){
            Toast.makeText(LockActivity.this,"密码正确",Toast.LENGTH_SHORT).show();
            finish();
        }else {
            Toast.makeText(LockActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
        }
        insertPwd="";
        clearCircle();
    }
    //清空圆点
    private void clearCircle() {
        for (ImageView circle:circles){
            circle.setVisibility(View.INVISIBLE);
        }
    }
    public void updateUi(){
        clearCircle();
        for (int i=0;i<insertPwd.length();i++){
            circles.get(i).setVisibility(View.VISIBLE);
        }
    }
    //如果是在锁界面点击返回则不产生反应
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}

注意,锁界面是不继承自BaseActivity的

源码下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,045评论 25 707
  • 1.什么是Activity?问的不太多,说点有深度的 四大组件之一,一般的,一个用户交互界面对应一个activit...
    JoonyLee阅读 5,721评论 2 51
  • 转自 1. 什么是Activity? 四大组件之一,一般的,一个用户交互界面对应一个activity setCon...
    joe1632阅读 1,380评论 0 7
  • Android Studio JNI流程首先在java代码声明本地方法 用到native关键字 本地方法不用去实现...
    MigrationUK阅读 11,816评论 7 123
  • 我的手机屏幕开始泛黄,在光线不好的情况下,四周一圈,淡淡的黄色,在夜里,中间还会见到五彩的星星,在论坛上,机油们称...
    浮木阅读 829评论 0 4