Android指纹解锁示例代码

所属分类: 软件编程 / Android 阅读数: 32
收藏 0 赞 0 分享

Android6.0及以上系统支持指纹识别解锁功能:项目中用到,特此抽离出来,备忘。

功能是这样的:在用户将app切换到后台运行(超过一定的时长,比方说30秒),再进入程序中的时候就会弹出指纹识别的界面。用户输入指纹,解锁成功。指纹识别的模块其实很简单啦,google的api已经封装好了,我们只需要学会调用就ok了。

思路:

在用户将程序切换到后台的时候需要有一个方法计时,这样的方法写在哪里呢,对,要写在service中。在Activity中开启服务:

Intent intent = new Intent("com.example.fingureprint.services.JudgeFingureService");
    intent.setPackage(getPackageName());
    startService(intent);

intent.setpackage(getPackageName);在6.0及以上系统中启动服务必须加上这句。

服务不要忘记在清单文件中注册幺。

 <!--注册判断指纹密码开启的服务-->
    <service 
      android:name="com.example.fingureprint.services.JudgeFingureService"
      android:enabled="true">
      <intent-filter >
        <action android:name="com.example.fingureprint.services.JudgeFingureService"/>
      </intent-filter>
    </service>

在服务中onStart()开启一个线程:

new Thread(){
      public void run() {
        try {
          while(true){
          Thread.sleep(1000);
          if(isAppOnForeground()){
            Log.i("前台运行", "time"+countnumber);
            if(countnumber>30){
              if(!"com.example.fingureprint.FingureAriseActivity".equals(listActivity())){
                Intent myintent = new Intent();
                myintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                myintent.setClass(getApplicationContext(),FingureAriseActivity.class);
                startActivity(myintent);
              }
              countnumber = 0;
            }else{
              countnumber = 0;
            }
          }else{
            Log.i("后台运行", "time"+countnumber);
            countnumber ++;
          }
          }
          
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      };
      
    }.start();

计时用了个while死循环,让thread睡一秒。哈哈,没有用Handler是不是很简单。

判断程序是否在后台运行:

  /**
   * 判断程序在前台运行的方法
   * @return
   */
  public boolean isAppOnForeground() {
    ActivityManager systemService = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> runningAppProcesses = systemService.getRunningAppProcesses();
    if(runningAppProcesses==null) return false;
    for(RunningAppProcessInfo processes: runningAppProcesses){
      if(processes.processName.equals("com.example.fingureprint")&&processes.importance==RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
        return true;
      }
    }
    return false;
  }

有一个小细节,当用户从后台切换到前台,但是用户没有解锁,又切换到后台,过三十秒后切换到前台,这时候不能再跳出手势解锁界面了吧。

解决:1.设置下手势界面的启动方式。2.检测程序的当前运行界面。

  /**
   * 判断当前运行在前台的Activity
   */
  @SuppressWarnings("deprecation")
  public String listActivity(){
    ActivityManager systemService = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> runningTasks = systemService.getRunningTasks(1);
    RunningTaskInfo runningTaskInfo = runningTasks.get(0);
    ComponentName component = runningTaskInfo.topActivity;
    String className = component.getClassName();
    return className;
  }

下面讲一下指纹识别的api:在6.0及以上的v4包下:

 需要在清单文件中添加权限:

<uses-permission android:name="android.permission.USE_FINGERPRINT"/>

用到的类有 FingerprintManagerCompat,此类中的api可以检测您的手机是否有指纹传感器,手机有没有录入过指纹。

AuthenticationCallback,可以继承此类,分别重写里面的方法,用来接收指纹识别的结果:

具体的可以下载下面的demo.

地址:demo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

android byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多