安卓指纹操作DEMO

package com.neusoft.phone.xinhua.newsedit.common.fingerprint;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import com.neusoft.phone.xinhua.newsedit.R;


public class FingerPrintActivity extends FragmentActivity {
    FingerprintManager manager;
    KeyguardManager mKeyManager;
    CancellationSignal mCancellationSignal;
    FingerprintManager.AuthenticationCallback mSelfCancelled;
    private final static int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 0;
    public final static int REQUEST_CODE_CHECKFINGER = 9;
    private final static String TAG = "finger_log";
    ImageView fingerImg;
    EditText fingerEdit;
    ImageButton back;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newsedit_activity_fingerprint);
        manager = (FingerprintManager) this.getSystemService(Context.FINGERPRINT_SERVICE);
        mKeyManager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
        initview();

        if (isFinger()) {
            //Toast.makeText(FingerPrintActivity.this, "您可以指纹解锁", Toast.LENGTH_LONG).show();
            initAuthenticationCallback();
            startListening(null);
            client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        }

    }

    private void initview() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            fingerImg = (ImageView) findViewById(R.id.newsedit_activity_fingerprint_img);//点击触发指纹操作控件
        }

        fingerEdit = (EditText) findViewById(R.id.newsedit_activity_fingerprint_edit);//输入手机解锁密码控件
        fingerEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                Log.i("监听文字变化:",""+s);
                if(s.toString().equals("123456"))
                {
                    Intent intent = new Intent();
                    setResult(REQUEST_CODE_CHECKFINGER,intent);
                    finish();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public boolean isFinger() {

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            //android studio 上,没有这个会报错
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "没有指纹识别权限", Toast.LENGTH_SHORT).show();
                return false;
            }
            //判断硬件是否支持指纹识别
            if (!manager.isHardwareDetected()) {
                Toast.makeText(this, "没有指纹识别模块", Toast.LENGTH_SHORT).show();
                return false;
            }

            //判断 是否开启锁屏密码

            if (!mKeyManager.isKeyguardSecure()) {
                Toast.makeText(this, "没有开启锁屏密码", Toast.LENGTH_SHORT).show();
                return false;
            }

            //判断是否有指纹录入
            if (!manager.hasEnrolledFingerprints()) {
                Toast.makeText(this, "没有录入指纹", Toast.LENGTH_SHORT).show();
                return false;
            }

            return true;
        } else {
            return false;
        }
    }

    @TargetApi(23)
    private void initAuthenticationCallback() {

        mCancellationSignal = new CancellationSignal();


        //回调方法

        mSelfCancelled = new FingerprintManager.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode, CharSequence errString) {
                //但多次指纹密码验证错误后,进入此方法;并且,不能短时间内调用指纹验证
                Toast.makeText(FingerPrintActivity.this, errString, Toast.LENGTH_SHORT).show();
                showAuthenticationScreen();
            }


            @Override
            public void onAuthenticationHelp(int helpCode, CharSequence helpString) {

                Toast.makeText(FingerPrintActivity.this, helpString, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
                Intent intent = new Intent();
                setResult(REQUEST_CODE_CHECKFINGER,intent);
                finish();
            }

            @Override
            public void onAuthenticationFailed() {
                Toast.makeText(FingerPrintActivity.this, "再试一次", Toast.LENGTH_SHORT).show();
            }
        };

    }

    @TargetApi(23)
    public void startListening(FingerprintManager.CryptoObject cryptoObject) {
        //android studio 上,没有这个会报错
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "没有指纹识别权限", Toast.LENGTH_SHORT).show();
            return;
        }

        manager.authenticate(cryptoObject, mCancellationSignal, 0, mSelfCancelled, null);

    }

    @TargetApi(23)
    /**
     * 锁屏密码
     */
    private void showAuthenticationScreen() {

        Intent intent = mKeyManager.createConfirmDeviceCredentialIntent("已锁定", "请输入手机解锁密码进行解锁");
        if (intent != null) {
            startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
            // Challenge completed, proceed with using cipher
            if (resultCode == RESULT_OK) {
                finish();
            } else {
                Toast.makeText(this, "再试一次", Toast.LENGTH_SHORT).show();
            }
        }
    }


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("FingerPrint Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }
    @Override
    public void onStart() {
        super.onStart();
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }
    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
}


package com.neusoft.phone.xinhua.newsedit.common.fingerprint.fingerprintutil;

/**
 * Created by paytend_liu on 2016/5/17.
 */
public class StaticManger {
    public final static String TAG = "finger_log";
}


package com.neusoft.phone.xinhua.newsedit.common.fingerprint.fingerprintutil;

import android.util.Log;

/**
 * Created by paytend_liu on 2016/5/17.
 */
public class UtilTools {
    public static void Log(String tag, String msg) {
        Log.d(tag, msg);
    }
}


阅读更多
文章标签: android 指纹
个人分类: 安卓学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭