Android – 收听传入的短信
我正在尝试创建一个应用程序来监视传入的SMS消息,并通过传入的SMS启动程序,也应该读取SMS的内容。
工作流程:
短信发送到Android设备自行执行应用程序读取短信息
如果有人能帮助我!
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" />
请注意,在一些设备上你的代码不会在没有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
:
总而言之,我的短信接收代码如下所示:
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" />