Facebook帐户工具包 - 在OTP屏幕UI,android上自动插入OTP代码

时间:2016-06-16 07:01:45

标签: android facebook account-kit

我正在使用Facebook AccountKit进行OTP验证,它的工作正常。

要求:通过读取传入的短信自动在OTP验证屏幕上设置OTP代码。

正在寻找:我是否可以通过编程方式将文字设置为OTP验证screen

我在搜索相同内容时没有找到任何答案Facebook是否在OTP验证屏幕上进行自定义,因为他们在第一个屏幕上提供相同的内容,用户可以在其中放置国家代码和手机号码。

4 个答案:

答案 0 :(得分:3)

Facebook为自动阅读OTP消息提供了两个boolean

AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                new AccountKitConfiguration.AccountKitConfigurationBuilder(LoginType.PHONE
                        , AccountKitActivity.ResponseType.TOKEN);

 // Add these code
 configurationBuilder.setReadPhoneStateEnabled(true);
 configurationBuilder.setReceiveSMS(true);

您还需要在AndroidManifest.xml文件中添加这两个权限:

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

API&gt; = 23:   检查是否授予了以上权限。

<强>更新

  

正如@markus建议的那样,请查看SMS permission updates

答案 1 :(得分:0)

AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                new AccountKitConfiguration.AccountKitConfigurationBuilder(LoginType.PHONE
                        , AccountKitActivity.ResponseType.TOKEN);

 // Add these two lines of code
 configurationBuilder.setReadPhoneStateEnabled(true);
 configurationBuilder.setReceiveSMS(true);

然后在manifiedt.xml文件中添加

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

然后,对于运行android M或更高版本的设备,您应该在运行时要求这些权限:您只需要询问android.permission.RECEIVE_SMS

答案 2 :(得分:0)

尝试使用Runtime Permission

只需在build.gradle中添加依赖项

implementation 'com.github.florent37:runtime-permission:(lastest version)'

然后,您需要在AndroidManifest.xml文件中添加以下两个权限:

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

然后在yourActivity中调用此方法:

  askPermission(this, Manifest.permission.READ_CONTACTS, Manifest.permission.READ_SMS)
            .ask(new PermissionListener() {

                @Override
                public void onAccepted(RuntimePermission runtimePermission, List<String> accepted) {

                }

                @Override
                public void onDenied(RuntimePermission runtimePermission, List<String> denied, List<String> foreverDenied) {
                    //the list of denied permissions
                    for (String permission : denied) {

                    }
                    //permission denied, but you can ask again, eg:

            /*
            new AlertDialog.Builder(MainActivity.this)
                    .setMessage("Please accept our permissions")
                    .setPositiveButton("yes", (dialog, which) -> { result.askAgain(); })
                    .setNegativeButton("no", (dialog, which) -> { dialog.dismiss(); })
                    .show();
            */

                    //the list of forever denied permissions, user has check 'never ask again'
                    for (String permission : foreverDenied) {

                    }
                    // you need to open setting manually if you really need it
                    //runtimePermission.goToSettings();
                }
            });

答案 3 :(得分:0)

将这两行添加到清单标记中的Android Manifest.xml文件中:

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

在MainActivity AppCompatActivity中定义新变量:

private static final String TAG = "MapActivity";
private static final String mREAD_SMS = Manifest.permission.READ_SMS;
private static final String mRECEIVE_SMS = Manifest.permission.RECEIVE_SMS;
private static final int SMS_PERMISSION_REQUEST_CODE = 1234;

定义一个名为getSmsPermission(){}

的新的私有void方法
private void getSmsPermission(){
    Log.d(TAG, "getSmsPermission: getting sms permissions");
    String[] permissions = {Manifest.permission.READ_SMS,
            Manifest.permission.RECEIVE_SMS};

    if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
            mREAD_SMS) == PackageManager.PERMISSION_GRANTED){
        if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
                mRECEIVE_SMS) == PackageManager.PERMISSION_GRANTED){
            //onActivityResult();
        }else{
            ActivityCompat.requestPermissions(this,
                    permissions,
                    SMS_PERMISSION_REQUEST_CODE);
        }
    }else{
        ActivityCompat.requestPermissions(this,
                permissions,
                SMS_PERMISSION_REQUEST_CODE);
    }
}

致电getSmsPermission();方法中称为startLoginSystem(){}的方法,或者您配置帐户工具包的位置。

private void startLoginSystem() {

    Intent intent = new Intent(MainActivity.this,AccountKitActivity.class);
    AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
            new AccountKitConfiguration.AccountKitConfigurationBuilder(LoginType.PHONE,
                    AccountKitActivity.ResponseType.TOKEN);

    intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,configurationBuilder.build());
    startActivityForResult(intent,REQUEST_CODE);

    //Asks for Sms Permission
    getSmsPermission();

    configurationBuilder.setReadPhoneStateEnabled(true);
    configurationBuilder.setReceiveSMS(true);
}

如下所示: https://drive.google.com/open?id=120sQ-pc4x1X1termHxkd7IKKRhQVI916