源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android实现双模(CDMA/GSM)手机短信监听的方法

  • 时间:2022-08-17 20:39 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android实现双模(CDMA/GSM)手机短信监听的方法
本文实例讲述了Android实现双模(CDMA/GSM)手机短信监听的方法。分享给大家供大家参考,具体如下: [b]一、问题分析:[/b] 最近在做一个通过短信远程启动应用的功能,要用到短信监听,代码如下:
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(); //得到短信发送者的手机号
}
}
}
}
}

实际应用时发现双模手机对接收到的短信处理时总是在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) 而在android的源码中可以看到createFromPdu方法:
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 当短信表发送改变时,调用该方法
*       需要两种权限
*<li>android.permission.READ_SMS读取短信 </li>
*<li>android.permission.WRITE_SMS写短信 </li>
* @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();
}
}
}
}

如果是双模手机,调用此方法时会产生错误,问题就在于源码的TelephonyManager.getDefault().getPhoneType();该方法的返回值没有对应的双模手机的类型,而原生的android系统是不支持双模手机的。 [b]二、解决办法:[/b] 我们可以采用广播接收者和内容观察者相结合的方式,直接读取手机的短信数据库,这样就避免了错误的产生,废话就不多说了,直接上代码:
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 当短信表发送改变时,调用该方法
*       需要两种权限
*       <li>android.permission.READ_SMS读取短信 </li>
*       <li>android.permission.WRITE_SMS写短信 </li>
* @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>

这样就能适应所有的android手机了,无论是双模还是单模都没问题,问题解决了。 更多关于Android相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/402.htm]Android数据库操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/367.htm]Android编程之activity操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/325.htm]Android文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/224.htm]Android编程开发之SD卡操作方法汇总[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/423.htm]Android资源操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部