效果:
![](https://img-blog.csdn.net/20170713195037101?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQklIVUEwOTE4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
Android studio中打印
![](https://img-blog.csdn.net/20170713195046976?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQklIVUEwOTE4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
1:实例化一个类MySMS.java
public class MySMS extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//判断手机动作是否为短信接收
//android.provider.Telephony.SMS_RECEIVED
if("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())){
//获得bundle
Bundle bundle=intent.getExtras();
//pdus是专门获得短信的,也必须写死
Object object[]= (Object[]) bundle.get("pdus");
//使用SmsMessage类
SmsMessage smsMessage[]=new SmsMessage[object.length];
for (int i = 0; i < object.length; i++) {
//将获得的值
smsMessage[i]=SmsMessage.createFromPdu((byte[]) object[i]);
}
//循环获得值
for (SmsMessage message : smsMessage) {
//获得谁打进来的
String address=message.getOriginatingAddress();
//获得短信的内容
String content=message.getDisplayMessageBody();
Log.i("test",address+":"+content);
}
}
}
}
2、配置信息中与activity同级
<receiver android:name=".MySMS">
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED"
>
</action>
</intent-filter>
</receiver>
3、配置中添加权限
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
Android studio中打印
1:实例化一个类MySMS.java
public class MySMS extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//判断手机动作是否为短信接收
//android.provider.Telephony.SMS_RECEIVED
if("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())){
//获得bundle
Bundle bundle=intent.getExtras();
//pdus是专门获得短信的,也必须写死
Object object[]= (Object[]) bundle.get("pdus");
//使用SmsMessage类
SmsMessage smsMessage[]=new SmsMessage[object.length];
for (int i = 0; i < object.length; i++) {
//将获得的值
smsMessage[i]=SmsMessage.createFromPdu((byte[]) object[i]);
}
//循环获得值
for (SmsMessage message : smsMessage) {
//获得谁打进来的
String address=message.getOriginatingAddress();
//获得短信的内容
String content=message.getDisplayMessageBody();
Log.i("test",address+":"+content);
}
}
}
}
2、配置信息中与activity同级
<receiver android:name=".MySMS">
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED"
>
</action>
</intent-filter>
</receiver>
3、配置中添加权限
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>