一、问题分析:
最近在做一个通过短信远程启动应用的功能,要用到短信监听,代码如下:
import
android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver
{
/* 当收到短信时,就会触发此方法 */
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle != null && bundle.get( " pdus " ) != null ){
Object[] pdus = (Object[]) bundle.get( " pdus " ); // 得到由短信内容组成的数组对象
if (pdus != null && pdus.length > 0 ){
SmsMessage[] messages = new SmsMessage[pdus.length];
for ( int i = 0 ;i < pdus.length;i ++ ){
byte [] pdu = ( byte []) pdus[i]; // 得到短信内容,内容是以pdu格式存放的
messages[i] = SmsMessage.createFromPdu(pdu);
}
for (SmsMessage msg:messages){
String smscontent = msg.getMessageBody(); // 得到短信内容
String smssender = msg.getOriginatingAddress(); // 得到短信发送者的手机号
}
}
}
}
}
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver
{
/* 当收到短信时,就会触发此方法 */
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle != null && bundle.get( " pdus " ) != null ){
Object[] pdus = (Object[]) bundle.get( " pdus " ); // 得到由短信内容组成的数组对象
if (pdus != null && pdus.length > 0 ){
SmsMessage[] messages = new SmsMessage[pdus.length];
for ( int i = 0 ;i < pdus.length;i ++ ){
byte [] pdu = ( byte []) pdus[i]; // 得到短信内容,内容是以pdu格式存放的
messages[i] = SmsMessage.createFromPdu(pdu);
}
for (SmsMessage msg:messages){
String smscontent = msg.getMessageBody(); // 得到短信内容
String smssender = msg.getOriginatingAddress(); // 得到短信发送者的手机号
}
}
}
}
}
实际应用时发现双模手机对接收到的短信处理时总是在SmsMessage.createFromPdu的地方出现异常,异常信息:
java.lang.OutOfMemoryError: array size too large
at com.android.internal.telephony.cdma.SmsMessage.parsePdu(SmsMessage.java:658)
at com.android.internal.telephony.cdma.SmsMessage.createFromPdu(SmsMessage.java:116)
at android.telephony.SmsMessage.createFromPdu(SmsMessage.java:162)
at com.android.internal.telephony.cdma.SmsMessage.parsePdu(SmsMessage.java:658)
at com.android.internal.telephony.cdma.SmsMessage.createFromPdu(SmsMessage.java:116)
at android.telephony.SmsMessage.createFromPdu(SmsMessage.java:162)
而在android的源码中可以看到createFromPdu方法:
public
static
SmsMessage createFromPdu(
byte
[] pdu) {
SmsMessageBase wrappedMessage;
int activePhone = TelephonyManager.getDefault().getPhoneType();
if (PHONE_TYPE_CDMA == activePhone) {
wrappedMessage = com.android.internal.telephony.cdma.SmsMessage.createFromPdu(pdu);
} else {
wrappedMessage = com.android.internal.telephony.gsm.SmsMessage.createFromPdu(pdu);
}
return new SmsMessage(wrappedMessage);
}
SmsMessageBase wrappedMessage;
int activePhone = TelephonyManager.getDefault().getPhoneType();
if (PHONE_TYPE_CDMA == activePhone) {
wrappedMessage = com.android.internal.telephony.cdma.SmsMessage.createFromPdu(pdu);
} else {
wrappedMessage = com.android.internal.telephony.gsm.SmsMessage.createFromPdu(pdu);
}
return new SmsMessage(wrappedMessage);
}
如果是双模手机,调用此方法时会产生错误,问题就在于源码的TelephonyManager.getDefault().getPhoneType();该方法的返回值没有对应的双模手机的类型,而原生的android系统是不支持双模手机的。
二、解决办法:
我们可以采用广播接收者和内容观察者相结合的方式,直接读取手机的短信数据库,这样就避免了错误的产生,废话就不多说了,直接上代码:
import
android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
public class SMSReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = " android.provider.Telephony.SMS_RECEIVED " ;
private Context m_Context;
private SmsContentObserver m_Smsobserver = new SmsContentObserver( new Handler());
@Override
public void onReceive(Context context, Intent intent) {
this .m_Context = context;
if (intent.getAction().equals(SMS_RECEIVED)) {
// 注册短信变化监听
context.getContentResolver().registerContentObserver(Uri.parse( " content://sms/ " ), true , m_Smsobserver);
}
}
/**
* 短信内容观察者
* @author sinber
*
*/
private class SmsContentObserver extends ContentObserver{
public SmsContentObserver(Handler handler) {
super (handler);
}
/**
* @Description 当短信表发送改变时,调用该方法
* 需要两种权限
*android.permission.READ_SMS读取短信
*android.permission.WRITE_SMS写短信
* @Author sinebr
*
*/
@Override
public void onChange( boolean selfChange) {
super .onChange(selfChange);
Cursor cursor = null ;
try {
// 读取收件箱中的短信
cursor = m_Context.getContentResolver().query(Uri.parse( " content://sms/inbox " ), null , null , null , " date desc " );
String body;
boolean hasDone = false ;
if (cursor != null ){
while (cursor.moveToNext()){
body = cursor.getString(cursor.getColumnIndex( " body " ));
if (body != null && body.equals( "【 startMyActivity】 " )){
// 此处略去启动应用的代码
hasDone = true ;
break ;
}
if (hasDone){
break ;
}
}
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (cursor != null )
cursor.close();
}
}
}
}
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
public class SMSReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = " android.provider.Telephony.SMS_RECEIVED " ;
private Context m_Context;
private SmsContentObserver m_Smsobserver = new SmsContentObserver( new Handler());
@Override
public void onReceive(Context context, Intent intent) {
this .m_Context = context;
if (intent.getAction().equals(SMS_RECEIVED)) {
// 注册短信变化监听
context.getContentResolver().registerContentObserver(Uri.parse( " content://sms/ " ), true , m_Smsobserver);
}
}
/**
* 短信内容观察者
* @author sinber
*
*/
private class SmsContentObserver extends ContentObserver{
public SmsContentObserver(Handler handler) {
super (handler);
}
/**
* @Description 当短信表发送改变时,调用该方法
* 需要两种权限
*
*
* @Author sinebr
*
*/
@Override
public void onChange( boolean selfChange) {
super .onChange(selfChange);
Cursor cursor = null ;
try {
// 读取收件箱中的短信
cursor = m_Context.getContentResolver().query(Uri.parse( " content://sms/inbox " ), null , null , null , " date desc " );
String body;
boolean hasDone = false ;
if (cursor != null ){
while (cursor.moveToNext()){
body = cursor.getString(cursor.getColumnIndex( " body " ));
if (body != null && body.equals( "【 startMyActivity】 " )){
// 此处略去启动应用的代码
hasDone = true ;
break ;
}
if (hasDone){
break ;
}
}
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (cursor != null )
cursor.close();
}
}
}
}
最后别忘了在AndroidManifest.xml中添加相应的权限,
< uses-permission android:name ="android.permission.RECEIVE_SMS" />
< uses-permission android:name ="android.permission.SEND_SMS" />
还有别忘了注册广播接收者:
<
receiver android:name
=
"
.SMSReceiver
"
>
< intent - filter >
< action android:name = " android.provider.Telephony.SMS_RECEIVED " />
intent - filter >
receiver >
< intent - filter >
< action android:name = " android.provider.Telephony.SMS_RECEIVED " />
intent - filter >
receiver >
这样就能适应所有的android手机了,无论是双模还是单模都没问题,问题解决了。