朋友,
我正在研究Kotlin
,并尝试在编辑文本中获取收到的SMS
。我不知道如何实现它。如果有人有正确的Kotlin
代码来获取OTP
,请在下面发布它,但是我已经按照下面的方法进行了实现并堆积在实现中,因此如果有人拥有,请在下面的Kotlin
代码中发布>
class SmsReceiver: BroadcastReceiver() {
private val mListener: SmsListener? = null
var b: Boolean? = null
var abcd: String? = null
var xyz:String? = null
@Suppress("DEPRECATION")
override fun onReceive(context: Context?, intent: Intent?) {
//val bundle = intent!!.getExtras()
val data = intent!!.extras
val pdus = data.get("pdus") as Array<Any>
}
}
以下是活动
public class OtpActivity extends Activity
{
EditText ed;
TextView tv;
String otp_generated,contactNo,id1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.otp);
ed=(EditText)findViewById(R.id.otp_et);
tv=(TextView) findViewById(R.id.verify_otp);
SmsReceiver.bin(new SmsListener() {
@Override
public void messageReceived(String messageText) {
ed.setText(messageText);
}
});
}
}
答案 0 :(得分:0)
这可以通过RxJava完成:
public static Observable<String> createMessageMonitor(@NonNull final Context context) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
return fromIntentFilter(context, intentFilter)
.filter(intent -> intent != null && intent.getExtras() != null)
.map(intent -> {
Bundle bundle = intent.getExtras();
Object[] pdu_Objects = (Object[]) bundle.get("pdus");
if (pdu_Objects != null) {
for (Object aObject : pdu_Objects) {
final SmsMessage currentSMS = getIncomingMessage(aObject, bundle);
final String senderNumber = currentSMS.getDisplayOriginatingAddress();
final String message = currentSMS.getDisplayMessageBody().trim();
Matcher matcher = OTP_PATTERN.matcher(message);
if (matcher.matches()) {
return matcher.group(1);
}
}
}
return "";
}).filter(message -> !message.isEmpty());
}
您的OTP_PATTERN:
private static Pattern OTP_PATTERN = Pattern.compile("^(\\d{4}) is the OTP for your App.$");
然后订阅该流:
RxBroadcastReceivers2.createMessageMonitor(this)
.subscribeWith(this,
onNext = { /*Your OTP is here*/ },
onError = {
Timber.e(it, "Registration permission request failed")
}
)
注意:在使用Manifest.permission.READ_SMS
方法之前,您需要先获得createMessageMonitor
。
答案 1 :(得分:0)
如果您不想编写接收器,则可以使用简单的轻量级库https://github.com/VitaliBov/SmsInterceptor
该库与Kotlin具有100%的兼容性
您只需要重写接口方法,创建一个Interceptor类并将其与生命周期绑定。看起来像这样:
SELECT
D.Enroll_Month
, cncl_mnth
, mth
, tot_orders
, tot_cancel
, tot_orders - total_cancel active_count
, 100 - CAST(CAST((tot_orders - total_cancel) AS DECIMAL(18,2) ) / (tot_orders) * 100 AS DECIMAL(18,2) ) attr_rate
, CAST(CAST((tot_orders - total_cancel) AS DECIMAL(18,2) ) / (tot_orders) * 100 AS DECIMAL(18,2) ) retn_rate
FROM (
SELECT
d.Enroll_Month
, d.cncl_mnth
, d.mth
, s.tot_orders
, d.tot_cancel
, SUM(d.tot_cancel) OVER(PARTITION BY d.Enroll_Month ORDER BY mth ROWS UNBOUNDED PRECEDING) total_cancel
FROM @summary s
JOIN @Detail d ON s.Enroll_Month = d.Enroll_Month
) D