发布于 2017-03-23 08:33:40 | 164 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android实现双模(CDMA/GSM)手机短信监听的方法,涉及Android短信的原理与相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android实现双模(CDMA/GSM)手机短信监听的方法。分享给大家供大家参考,具体如下:

一、问题分析:

最近在做一个通过短信远程启动应用的功能,要用到短信监听,代码如下:


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系统是不支持双模手机的。

二、解决办法:

我们可以采用广播接收者和内容观察者相结合的方式,直接读取手机的短信数据库,这样就避免了错误的产生,废话就不多说了,直接上代码:


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程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务