我希望在我的应用中添加短信阅读功能。用户在活动中输入他的移动号码,然后从服务器向用户移动电话发送包含一次性密码的短信。我想要做的是在用户输入手机号码并且期待来自服务器的传入消息后,触发应用程序读取传入的消息。
我在这里看到过这样的问题 How to read the incoming message using service in background in android?
但解决方案似乎是阅读手机上的所有短信。在我的情况下,我只希望读取功能仅在用户期望短信时开始,并在收到短信时结束。
答案 0 :(得分:1)
查看此答案,向您展示如何注册接收器 https://stackoverflow.com/a/7049747/774944
final String SOME_ACTION = "android.provider.Telephony.SMS_RECEIVED";
IntentFilter intentFilter = new IntentFilter(SOME_ACTION);
SMSReceiver mReceiver = new SMSReceiver();
registerReceiver(mReceiver,intentFilter);
答案 1 :(得分:0)
您可以使用包管理器在运行时启用/禁用广播接收器。
public void enableSmsModule()
{
PackageManager pm = getPackageManager();
ComponentName componentName = new ComponentName(getApplicationContext(), SMSReceiver.class);
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
public void disableSmsModule()
{
PackageManager pm = getPackageManager();
ComponentName componentName = new ComponentName(getApplicationContext(), SMSReceiver.class);
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
读取短信的代码:How to read the incoming message using service in background in android?
答案 2 :(得分:0)
在android中自动读取传入消息以验证OTP
public class IncomingSms extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
final Bundle bundle = intent.getExtras();
try {
if (bundle != null)
{
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj .length; i++)
{
SmsMessage currentMessage =
SmsMessage.createFromPdu((byte[])pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber ;
String message = currentMessage .getDisplayMessageBody();
try
{
if (senderNum .equals("TA-DOCOMO"))
{
Otp Sms = new Otp();
Sms.recivedSms(message );
}
}
catch(Exception e){}
}
}
} catch (Exception e)
{
}
}
}
如需更多参考,请点击此处http://androiddhina.blogspot.in/2015/06/reading-incoming-message-automatically-to-verify-OTP.html