Intent intent = new Intent("AddedItem");
intent.putExtra("items", code);
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
以及接收它的碎片/活动
@Override
public void onResume() {
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, new IntentFilter("AddedItem"));
super.onResume();
}
@Override
public void onPause() {
super.onDestroy();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
}
代码是用来处理你收集的有效载荷的
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()) {
final String message = intent.getStringExtra("message");
//Do whatever you want with the code here
}
}
};
private fun startSMSRetriever() {
// Get an instance of SmsRetrieverClient, used to start listening for a matching SMS message.
val client = SmsRetriever.getClient(this /* context */);
// Starts SmsRetriever, which waits for ONE matching SMS message until timeout
// (5 minutes). The matching SMS message will be sent via a Broadcast Intent with
// action SmsRetriever#SMS_RETRIEVED_ACTION.
val task: Task<Void> = client.startSmsRetriever();
// Listen for success/failure of the start Task. If in a background thread, this
// can be made blocking using Tasks.await(task, [timeout]);
task.addOnSuccessListener {
Log.d("SmsRetriever", "SmsRetriever Start Success")
}
task.addOnFailureListener {
Log.d("SmsRetriever", "SmsRetriever Start Failed")
}
}
通过广播接收消息
public class MySMSBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent?.action && intent.extras!=null) {
val extras = intent.extras
val status = extras.get(SmsRetriever.EXTRA_STATUS) as Status
when (status.statusCode) {
CommonStatusCodes.SUCCESS -> {
// Get SMS message contents
val message = extras.get(SmsRetriever.EXTRA_SMS_MESSAGE) as String
Log.e("Message", message);
// Extract one-time code from the message and complete verification
// by sending the code back to your server.
}
CommonStatusCodes.TIMEOUT -> {
// Waiting for SMS timed out (5 minutes)
// Handle the error ...
}
}
}
}
}
/**Don't forgot to define BroadcastReceiver in AndroidManifest.xml.*/
<receiver android:name=".MySMSBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
</intent-filter>
</receiver>
import android.content.Context
import android.content.ContextWrapper
import android.content.pm.PackageManager
import android.util.Base64
import android.util.Log
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.util.*
/**
* This is a helper class to generate your message hash to be included in your SMS message.
*
* Without the correct hash, your app won't recieve the message callback. This only needs to be
* generated once per app and stored. Then you can remove this helper class from your code.
*
* For More Detail: https://developers.google.com/identity/sms-retriever/verify#computing_your_apps_hash_string
*
*/
public class AppSignatureHelper(private val context: Context) : ContextWrapper(context) {
companion object {
val TAG = AppSignatureHelper::class.java.simpleName;
private const val HASH_TYPE = "SHA-256";
const val NUM_HASHED_BYTES = 9;
const val NUM_BASE64_CHAR = 11;
}
/**
* Get all the app signatures for the current package
* @return
*/
public fun getAppSignatures(): ArrayList<String> {
val appCodes = ArrayList<String>();
try {
// Get all package signatures for the current package
val signatures = packageManager.getPackageInfo(
packageName,
PackageManager.GET_SIGNATURES
).signatures;
// For each signature create a compatible hash
for (signature in signatures) {
val hash = hash(packageName, signature.toCharsString());
if (hash != null) {
appCodes.add(String.format("%s", hash));
}
}
} catch (e: PackageManager.NameNotFoundException) {
Log.e(TAG, "Unable to find package to obtain hash.", e);
}
return appCodes;
}
private fun hash(packageName: String, signature: String): String? {
val appInfo = "$packageName $signature";
try {
val messageDigest = MessageDigest.getInstance(HASH_TYPE);
messageDigest.update(appInfo.toByteArray(StandardCharsets.UTF_8));
var hashSignature = messageDigest.digest();
// truncated into NUM_HASHED_BYTES
hashSignature = Arrays.copyOfRange(hashSignature, 0, NUM_HASHED_BYTES);
// encode into Base64
var base64Hash = Base64.encodeToString(hashSignature, Base64.NO_PADDING or Base64.NO_WRAP);
base64Hash = base64Hash.substring(0, NUM_BASE64_CHAR);
Log.e(TAG, String.format("pkg: %s -- hash: %s", packageName, base64Hash));
return base64Hash;
} catch (e: NoSuchAlgorithmException) {
Log.e(TAG, "hash:NoSuchAlgorithm", e);
}
return null;
}
}
smsVerifyCatcher = new SmsVerifyCatcher(this, new OnSmsCatchListener<String>() {
@Override
public void onSmsCatch(String message) {
String code = parseCode(message);//Parse verification code
etCode.setText(code);//set code in edit text
//then you can send verification code to server
}
});
另外,重写活动生命周期方法:
@Override
protected void onStart() {
super.onStart();
smsVerifyCatcher.onStart();
}
@Override
protected void onStop() {
super.onStop();
smsVerifyCatcher.onStop();
}
/**
* need for Android 6 real time permissions
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
smsVerifyCatcher.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
public String parseCode(String message) {
Pattern p = Pattern.compile("\\b\\d{4}\\b");
Matcher m = p.matcher(message);
String code = "";
while (m.find()) {
code = m.group(0);
}
return code;
}
8条答案
按热度按时间aij0ehis1#
是的,这在浏览器中也是可能的。chrome在84及以上版本中发布了此功能。借助webotpapi,我们可以在web上检测移动设备的otp。
以下是一个与pwa应用程序集成的web otp代码:https://github.com/rohit3230/webotpautoreadbyangular
去现场工作的应用程序网址。https://rohit3230.github.io/webotpautoreadbyangular/
798qvoo82#
我实现了这样的东西。但是,当消息传入时,我只检索六位代码,将其捆绑在一个intent中,并将其发送给需要它的活动或片段,并验证代码。这个例子向您展示了如何获取短信。请看下面的代码,以了解如何使用localbrodcastmanager发送邮件,如果您的邮件包含更多文本(例如问候语),请对其进行标准化以更好地帮助您。e、 g“您的验证码是:84hg73”您可以创建这样的regex模式
([0-9]){2}([A-Z]){2}([0-9]){2}
意思是两个整数,两个大写字母和两个整数。祝你好运!丢弃邮件中所有不需要的信息后
以及接收它的碎片/活动
代码是用来处理你收集的有效载荷的
这有帮助吗。我用回调做得更好
piztneat3#
使用sms retriever api,可以读取otp而无需声明
android.permission.READ_SMS
.启动短信检索器
通过广播接收消息
将验证消息中的一次性代码发送到服务器
确保您的短信格式完全符合以下要求:
不超过140字节
以前缀开始
以11个字符的哈希字符串结束,该字符串标识您的应用程序
可以使用以下代码计算应用程序哈希:
所需等级:
参考文献:
https://developers.google.com/identity/sms-retriever/overview
https://developers.google.com/identity/sms-retriever/request
https://developers.google.com/identity/sms-retriever/verify
0vvn1miw4#
zbq4xfa05#
由于google限制了read\u sms权限的使用,这里是一个没有read\u sms权限的解决方案。
sms检索器api
基本功能是避免使用android临界权限读写短信,用这种方法完成任务。吹是你需要的步骤。
发送otp到用户号后,检查短信检索器api是否能得到消息
广播接收器代码
最后一步。将此收件人注册到您的清单中
你的短信必须如下。
这里qwsa8754qw2是您自己的应用程序11个字符的哈希代码。点击此链接
不超过140字节
以前缀开始
以11个字符的哈希字符串结束,该字符串标识您的应用程序
导入
com.google.android.gms.auth.api.phone.SmsRetriever
,别忘了将这一行添加到你的app build.gradle:ego6inou6#
你可以尝试使用一个简单的库,比如
通过gradle安装并添加权限后,在oncreate activity这样的方法中启动smsverifycatcher:
另外,重写活动生命周期方法:
knsnq2tg7#
抱歉,迟来的答复,但仍然想张贴我的答案,如果它有帮助。它为6位otp工作。
对于sms侦听器,可以按照下面的类
jjhzyzn08#
我建议您不要使用任何第三方库自动获取短信收件箱的otp。如果您对广播接收器及其工作原理有基本的了解,这是很容易做到的。请尝试以下方法:
步骤1)创建单个接口,即smslistner
步骤2)创建单个广播接收器,即smsreceiver
步骤3)在android清单文件中添加监听器,即广播接收器
并添加权限
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
最后一步4)当收到收件箱中的otp时,您将自动获取otp的活动。在我的例子中,我获取otp并设置edittext字段。OTP验证活动的布局文件
otp验证活动的屏幕截图,您可以在收到消息后立即获取otp