先把异常贴出来吧,参考下面的代码:
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(LOG_TAG, "onReceive SMS");
// 第一步、获取短信的内容和发件人
StringBuilder body = new StringBuilder();// 短信内容
StringBuilder number = new StringBuilder();// 短信发件人
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] _pdus = (Object[]) bundle.get("pdus");
SmsMessage[] message = new SmsMessage[_pdus.length];
for (int i = 0; i < _pdus.length; i++)
{
message[i] = SmsMessage.createFromPdu((byte[]) _pdus[i]);
}
for (SmsMessage currentMessage : message)
{
body.append(currentMessage.getDisplayMessageBody());
number.append(currentMessage.getDisplayOriginatingAddress());
}
String smsBody = body.toString();
String smsNumber = number.toString();
Log.d(LOG_TAG, smsNumber + ", " + smsBody); String smsTrialCode = null; String smsPrefix = context.getString(R.string.bmptrial_sms_host) + "/";
if (smsBody.contains(smsPrefix)) //我们公司的短信
{
// 屏蔽10086发来的短信
try
{
int indexOfPix = smsBody.indexOf(smsPrefix);
int index = indexOfPix + smsPrefix.length();
smsTrialCode = smsBody.substring(index, index + 4);
Log.i(LOG_TAG, "SMS Trial Code: " + smsTrialCode);
}
catch (Exception e)
{
Log.e(LOG_TAG, "SMS Exception: " + e.toString());
e.printStackTrace();
}
}
}
}
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(LOG_TAG, "onReceive SMS");
// 第一步、获取短信的内容和发件人
StringBuilder body = new StringBuilder();// 短信内容
StringBuilder number = new StringBuilder();// 短信发件人
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] _pdus = (Object[]) bundle.get("pdus");
SmsMessage[] message = new SmsMessage[_pdus.length];
for (int i = 0; i < _pdus.length; i++)
{
message[i] = SmsMessage.createFromPdu((byte[]) _pdus[i]);
}
for (SmsMessage currentMessage : message)
{
body.append(currentMessage.getDisplayMessageBody());
number.append(currentMessage.getDisplayOriginatingAddress());
}
String smsBody = body.toString();
String smsNumber = number.toString();
Log.d(LOG_TAG, smsNumber + ", " + smsBody); String smsTrialCode = null; String smsPrefix = context.getString(R.string.bmptrial_sms_host) + "/";
if (smsBody.contains(smsPrefix)) //我们公司的短信
{
// 屏蔽10086发来的短信
try
{
int indexOfPix = smsBody.indexOf(smsPrefix);
int index = indexOfPix + smsPrefix.length();
smsTrialCode = smsBody.substring(index, index + 4);
Log.i(LOG_TAG, "SMS Trial Code: " + smsTrialCode);
}
catch (Exception e)
{
Log.e(LOG_TAG, "SMS Exception: " + e.toString());
e.printStackTrace();
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货