看我七十二变----巧用Android手机指纹识别器扩充手势识别功能(二)



    现在把相关代码做下说明,大家就对这个实现方式更清楚了:

    程序一共有三个类,分别为MainActivity(主界面),FingerClickService(无障碍服务)和FingerClickCallBack(指纹识别器的回调类)

     

    AndroidMainfest.xml 主要需要配置:

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

   对应指纹识别器的权限

      

 <service android:name=".FingerClickService"
            android:label="@string/accessibility_service_label"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>
        </service>

对应无障碍服务的配置,其中android:name名称为“.FingerClickService”,类名前要有一个点,且类名要跟你程序里面无障碍服务的类名一致

   

    MainActivity类:

   

 private static final Intent sSettingsIntent =
		        new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btRegister = (Button)findViewById(R.id.btn1);
		
		btRegister.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(sSettingsIntent);
			}
			
		});	
	}

     主要是注册无障碍服务;


     FingerClickService类,主要实现具体的无障碍服务类,一个是在程序中安装指纹识别功能,第二个是监听解锁信息,避免正常指纹解锁后,程序再也无法读取指纹

    

protected void onServiceConnected() {
		// TODO Auto-generated method stub

		/* 安装指纹识别 */
		if (fingerSensor == null) {
			fingerSensor = new FingerSensorCallBack();
			fingerSensor.init(this);
			fingerMgr = FingerprintManagerCompat.from(this);

			scnMsgReciever = new ScreenMsgReciever();
			IntentFilter filter = new IntentFilter();
			filter.addAction(Intent.ACTION_SCREEN_ON);
			filter.addAction(intentName);
			registerReceiver(scnMsgReciever, filter);
		}

		mCancellationSignal = new android.support.v4.os.CancellationSignal();
		fingerMgr.authenticate(null, 0, mCancellationSignal, fingerSensor, null);

		Log.d("FingerClick-onServiceConnected", "Install success!");
	}


这个函数主要是安装指纹识别功能,同时准备接收两个Intent,一个是接收解锁的广播,一个是指纹输错后达到一定次数,会被系统禁止掉,需要重新启动的指纹识别器的广播。

public class ScreenMsgReciever extends BroadcastReceiver {
		// 解锁进入系统,重新监听
		@Override
		public void onReceive(Context context, Intent intent) {
			if (mCancellationSignal != null) {
				mCancellationSignal.cancel();
			}

			mCancellationSignal = new android.support.v4.os.CancellationSignal();
			fingerMgr.authenticate(null, 0, mCancellationSignal, fingerSensor,
					null);

		}
	}

接收上面定义的两个Intent;主要是重启指纹识别器

    类FingerSensorCallBack,具体的指纹回调处理函数,发送点击动作等就是在这个类发送的

  @Override
    public void onAuthenticationFailed() {
    	sendClickNotisfications();
    	 
        errCount = errCount + 1;
        if(errCount == 3) {
        	 context.sendBroadcast(new Intent(context.getIntentName()));
        	 errCount = 0;
        }
    }

    @Override
    public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
    	if(helpMsgId == 5) {
    		sendClickRecent();
    		bNotifycationsOpen = false;
    	}
     }

   
    @Override
    public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult
                                                  result) {
    	sendClickNotisfications();
    	context.sendBroadcast(new Intent(context.getIntentName()));
    }
    
    private void  sendClickNotisfications() {
    	if(bNotifycationsOpen == false) {
    	    context.performGlobalAction(AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS);
    	    bNotifycationsOpen = true;
    	} else {
    		context.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
    		bNotifycationsOpen = false;
    	}
    }
    
    private void sendClickRecent() {
    	context.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);
    	context.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);
    }

对于指纹刷入错误三次,就需要重启指纹服务,sendClickRecent发送两次点击事件就可以切换程序了,这个是Android7.0以上系统自带的功能。


小结:

1、以上功能验证环境为,Android7.1.1beta版, Nexus6P;

2、开发环境为:eclipse+ADT24+SDK23,没用android studio,是因为AccessibilityService服务加载时,在as中一直提示class loader错误,无法调试,排查很久发现是as的一个bug,因此改用eclipse开发;

3、这个功能个人感觉很有用,大屏手机有后置指纹识别器的时候,能够解决不少在单手操作是,需要看通知和切换程序的问题;




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值