Answers:
public class SmsListener extends BroadcastReceiver{
private SharedPreferences preferences;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
}
}catch(Exception e){
// Log.d("Exception caught",e.getMessage());
}
}
}
}
}
注意:在清单文件中,添加BroadcastReceiver-
<receiver android:name=".listener.SmsListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
添加此权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
SMS_RECEIVED
在4.4+版本中获得广播,并且由于该广播不能中断,因此比以前的版本更确定。
请注意,在某些设备上,如果没有intent过滤器中的android:priority =“ 1000”,则您的代码将无法工作:
<receiver android:name=".listener.SmsListener">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
这是一些优化:
public class SmsListener extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
String messageBody = smsMessage.getMessageBody();
}
}
}
}
注意:
该值必须是整数,例如“ 100”。数字越高优先级越高。缺省值为0。该值必须大于-1000并且小于1000。
@Mike M.和我发现了可接受的答案存在问题(请参阅我们的评论):
基本上,如果我们没有每次都串联多部分消息,那么遍历for循环是没有意义的:
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
}
请注意,msgBody
无论我们位于什么索引上,我们都只是将其设置为消息各个部分的字符串值,这使遍历SMS消息不同部分的整个过程变得毫无用处,因为它将仅设置为最后的索引值。相反,我们应该使用+=
,或者如Mike指出的那样StringBuilder
:
总而言之,这是我的SMS接收代码如下所示:
if (myBundle != null) {
Object[] pdus = (Object[]) myBundle.get("pdus"); // pdus is key for SMS in bundle
//Object [] pdus now contains array of bytes
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); //Returns one message, in array because multipart message due to sms max char
Message += messages[i].getMessageBody(); // Using +=, because need to add multipart from before also
}
contactNumber = messages[0].getOriginatingAddress(); //This could also be inside the loop, but there is no need
}
只是把这个答案放在那里,以防其他人有同样的困惑。
这就是我用的!
public class SMSListener extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
String mobile,body;
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the 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();
mobile=senderNum.replaceAll("\\s","");
body=message.replaceAll("\\s","+");
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + body);
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ mobile+ ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}
如果要处理打开的活动的意图,则可以使用PendintIntent(完成以下步骤):
public class SMSReciver 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.contains("MOB_NUMBER")) {
Toast.makeText(context,"",Toast.LENGTH_SHORT).show();
Intent intentCall = new Intent(context, MainActivity.class);
intentCall.putExtra("message", currentMessage.getMessageBody());
PendingIntent pendingIntent= PendingIntent.getActivity(context, 0, intentCall, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.send();
}
} catch (Exception e) {
}
}
}
} catch (Exception e) {
}
}
}
表现:
<activity android:name=".MainActivity"
android:launchMode="singleTask"/>
<receiver android:name=".SMSReciver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
onNewIntent:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Toast.makeText(this, "onNewIntent", Toast.LENGTH_SHORT).show();
onSMSReceived(intent.getStringExtra("message"));
}
权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
如果有人像我一样引用如何在Xamarin Android上执行相同的功能(使用接收的SMS读取OTP):
将此代码添加到您的AndroidManifest.xml文件中:
<receiver android:name=".listener.BroadcastReveiverOTP">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.BROADCAST_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
然后在您的Android项目中创建您的BroadcastReveiver类。
[BroadcastReceiver(Enabled = true)] [IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority = (int)IntentFilterPriority.HighPriority)]
public class BroadcastReveiverOTP : BroadcastReceiver {
public static readonly string INTENT_ACTION = "android.provider.Telephony.SMS_RECEIVED";
protected string message, address = string.Empty;
public override void OnReceive(Context context, Intent intent)
{
if (intent.HasExtra("pdus"))
{
var smsArray = (Java.Lang.Object[])intent.Extras.Get("pdus");
foreach (var item in smsArray)
{
var sms = SmsMessage.CreateFromPdu((byte[])item);
address = sms.OriginatingAddress;
if (address.Equals("NotifyDEMO"))
{
message = sms.MessageBody;
string[] pin = message.Split(' ');
if (!string.IsNullOrWhiteSpace(pin[0]))
{
// NOTE : Here I'm passing received OTP to Portable Project using MessagingCenter. So I can display the OTP in the relevant entry field.
MessagingCenter.Send<object, string>(this,MessengerKeys.OnBroadcastReceived, pin[0]);
}
}
}
}
}
}
在Android Project的MainActivity类中注册此BroadcastReceiver类:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {
// Initialize your class
private BroadcastReveiverOTP _receiver = new BroadcastReveiverOTP ();
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
// Register your receiver : RegisterReceiver(_receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
}
}
感谢@Vineet Shukla(接受的答案)和@Ruchir Baronia(在接受的答案中发现了问题),以下是Kotlin
版本:
添加权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
在AndroidManifest中注册BroadcastReceiver:
<receiver
android:name=".receiver.SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="2332412">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
添加BroadcastReceiver的实现:
class SmsReceiver : BroadcastReceiver() {
private var mLastTimeReceived = System.currentTimeMillis()
override fun onReceive(p0: Context?, intent: Intent?) {
val currentTimeMillis = System.currentTimeMillis()
if (currentTimeMillis - mLastTimeReceived > 200) {
mLastTimeReceived = currentTimeMillis
val pdus: Array<*>
val msgs: Array<SmsMessage?>
var msgFrom: String?
var msgText: String?
val strBuilder = StringBuilder()
intent?.extras?.let {
try {
pdus = it.get("pdus") as Array<*>
msgs = arrayOfNulls(pdus.size)
for (i in msgs.indices) {
msgs[i] = SmsMessage.createFromPdu(pdus[i] as ByteArray)
strBuilder.append(msgs[i]?.messageBody)
}
msgText = strBuilder.toString()
msgFrom = msgs[0]?.originatingAddress
if (!msgFrom.isNullOrBlank() && !msgText.isNullOrBlank()) {
//
// Do some thing here
//
}
} catch (e: Exception) {
}
}
}
}
}
有时事件会触发两次,所以我添加 mLastTimeReceived = System.currentTimeMillis()
Kotlin上的广播实施:
private class SmsListener : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(TAG, "SMS Received!")
val txt = getTextFromSms(intent?.extras)
Log.d(TAG, "message=" + txt)
}
private fun getTextFromSms(extras: Bundle?): String {
val pdus = extras?.get("pdus") as Array<*>
val format = extras.getString("format")
var txt = ""
for (pdu in pdus) {
val smsmsg = getSmsMsg(pdu as ByteArray?, format)
val submsg = smsmsg?.displayMessageBody
submsg?.let { txt = "$txt$it" }
}
return txt
}
private fun getSmsMsg(pdu: ByteArray?, format: String?): SmsMessage? {
return when {
SDK_INT >= Build.VERSION_CODES.M -> SmsMessage.createFromPdu(pdu, format)
else -> SmsMessage.createFromPdu(pdu)
}
}
companion object {
private val TAG = SmsListener::class.java.simpleName
}
}
注意:在清单文件中,添加BroadcastReceiver-
<receiver android:name=".listener.SmsListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
添加此权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
可接受的答案是正确的,并且可以在较旧版本的Android上运行,在较旧版本的Android上,Android OS会在应用安装时要求权限;但是,在较新版本的Android上,它不会立即起作用,因为较新的Android OS在运行时会在应用需要该功能时要求权限。因此,为了使用接受的答案中提到的技术在更新版本的Android上接收SMS,程序员还必须实现将在运行时检查并向用户请求权限的代码。在这种情况下,可以在应用程序的第一个活动的onCreate()中实现权限检查功能/代码。只需在第一个活动中复制并粘贴以下两个方法,然后在onCreate()的末尾调用checkForSmsReceivePermissions()方法。
void checkForSmsReceivePermissions(){
// Check if App already has permissions for receiving SMS
if(ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.RECEIVE_SMS") == PackageManager.PERMISSION_GRANTED) {
// App has permissions to listen incoming SMS messages
Log.d("adnan", "checkForSmsReceivePermissions: Allowed");
} else {
// App don't have permissions to listen incoming SMS messages
Log.d("adnan", "checkForSmsReceivePermissions: Denied");
// Request permissions from user
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.RECEIVE_SMS}, 43391);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 43391){
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.d("adnan", "Sms Receive Permissions granted");
} else {
Log.d("adnan", "Sms Receive Permissions denied");
}
}
}