Android手机上监听短信的两种方式
Android手机上监听短信有两种方式:
1、 接受系统的短信广播,操作短信内容。
优点:操作方便,适合简单的短信应用。
缺点:来信会在状态栏显示通知信息。
AndroidManifest.xml:
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission> <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> <!-- Receiver --> <receiver android:name=".sms.SMSReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> |
code:
package org.anymobile.demo.sms; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.gsm.SmsMessage; import android.util.Log; public class SMSReceiver extends BroadcastReceiver { public static final String TAG = "ImiChatSMSReceiver"; //android.provider.Telephony.Sms.Intents public static final String SMS_RECEIVED_ACTION ="android.provider.Telephony.SMS_RECEIVED"; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(SMS_RECEIVED_ACTION)) { SmsMessage[] messages = getMessagesFromIntent(intent); for (SmsMessage message : messages) { Log.i(TAG, message.getOriginatingAddress() + " : " + message.getDisplayOriginatingAddress() + " : " + message.getDisplayMessageBody() + " : " + message.getTimestampMillis()); } } } public final SmsMessage[] getMessagesFromIntent(Intent intent) { Object[] messages = (Object[]) intent.getSerializableExtra("pdus"); byte[][] pduObjs = new byte[messages.length][]; for (int i = 0; i < messages.length; i++) { pduObjs[i] = (byte[]) messages[i]; } byte[][] pdus = new byte[pduObjs.length][]; int pduCount = pdus.length; SmsMessage[] msgs = new SmsMessage[pduCount]; for (int i = 0; i < pduCount; i++) { pdus[i] = pduObjs[i]; msgs[i] = SmsMessage.createFromPdu(pdus[i]); } return msgs; } } |
2、 应用观察者模式,监听短信数据库,操作短信内容。
优点:可以拦截来信在状态栏的显示通知,适合作短信拦截。
缺点:可以发展成MU,在后台悄悄的收/发短信。
实例如下:
SystemEventReceiver:收到开机启动OK的广播后,启动BootService服务;收到应用发送短信的INTENT后,调用系统接口发送短信
BootService:开启服务后,打开短信监听器
SMSObserver:数据观察者,监听短信数据库
SMSHandler:短信处理器
AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_SMS"></uses-permission> <uses-permission android:name="android.permission.WRITE_SMS"></uses-permission> <!-- Service --> <service android:name=".BootService" android:label="@string/app_name"> <intent-filter> <actionandroid:name="org.anymobile.test.service.IMICHAT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service> <!-- Receiver --> <receiver android:name=".SystemEventReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> |
code:
package org.anymobile.demo.sms; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Handler; import android.os.Message; import android.util.Log; public class SMSHandler extends Handler { public static final String TAG = "SMSHandler"; private Context mContext; public SMSHandler(Context context) { super(); this.mContext = context; } public void handleMessage(Message message) { Log.i(TAG, "handleMessage: " + message); MessageItem item = (MessageItem) message.obj; //delete the sms Uri uri = ContentUris.withAppendedId(SMS.CONTENT_URI, item.getId()); mContext.getContentResolver().delete(uri, null, null); Log.i(TAG, "delete sms item: " + item); } } |
package org.anymobile.demo.sms; import android.content.ContentResolver; import android.database.ContentObserver; import android.database.Cursor; import android.os.Handler; import android.os.Message; import android.util.Log; public class SMSObserver extends ContentObserver { public static final String TAG = "SMSObserver"; private static final String[] PROJECTION = new String[] { SMS._ID,//0 SMS.TYPE,//1 SMS.ADDRESS,//2 SMS.BODY,//3 SMS.DATE,//4 SMS.THREAD_ID,//5 SMS.READ,//6 SMS.PROTOCOL//7 }; private static final String SELECTION = SMS._ID + " > %s" + // " and " + SMS.PROTOCOL + " = null" + // " or " + SMS.PROTOCOL + " = " + SMS.PROTOCOL_SMS + ")" + " and (" + SMS.TYPE + " = " + SMS.MESSAGE_TYPE_INBOX + " or " + SMS.TYPE + " = " + SMS.MESSAGE_TYPE_SENT + ")"; private static final int COLUMN_INDEX_ID = 0; private static final int COLUMN_INDEX_TYPE = 1; private static final int COLUMN_INDEX_PHONE = 2; private static final int COLUMN_INDEX_BODY = 3; private static final int COLUMN_INDEX_PROTOCOL = 7; private static final int MAX_NUMS = 10; private static int MAX_ID = 0; private ContentResolver mResolver; private Handler mHandler; public SMSObserver(ContentResolver contentResolver, Handler handler) { super(handler); this.mResolver = contentResolver; this.mHandler = handler; } @Override public void onChange(boolean selfChange) { Log.i(TAG, "onChange : " + selfChange + "; " + MAX_ID + "; " +SELECTION); super.onChange(selfChange); Cursor cursor = mResolver.query(SMS.CONTENT_URI, PROJECTION, String.format(SELECTION, MAX_ID), null, null); int id, type, protocol; String phone, body; Message message; MessageItem item; int iter = 0; boolean hasDone = false; while (cursor.moveToNext()) { id = cursor.getInt(COLUMN_INDEX_ID); type = cursor.getInt(COLUMN_INDEX_TYPE); phone = cursor.getString(COLUMN_INDEX_PHONE); body = cursor.getString(COLUMN_INDEX_BODY); protocol = cursor.getInt(COLUMN_INDEX_PROTOCOL); if (hasDone) { MAX_ID = id; break; } if (protocol == SMS.PROTOCOL_SMS && body != null && body.startsWith(SMS.FILTER)) { hasDone = true; item = new MessageItem(); item.setId(id); item.setType(type); item.setPhone(phone); item.setBody(body); item.setProtocol(protocol); message = new Message(); message.obj = item; mHandler.sendMessage(message); } else { if (id > MAX_ID) MAX_ID = id; } if (iter > MAX_NUMS) break; iter ++; } } } |
package org.anymobile.demo.sms; import android.net.Uri; import android.provider.BaseColumns; public interface SMS extends BaseColumns { public static final Uri CONTENT_URI = Uri.parse("content://sms"); public static final String FILTER = "!imichat"; public static final String TYPE = "type"; public static final String THREAD_ID = "thread_id"; public static final String ADDRESS = "address"; public static final String PERSON_ID = "person"; public static final String DATE = "date"; public static final String READ = "read"; public static final String BODY = "body"; public static final String PROTOCOL = "protocol"; public static final int MESSAGE_TYPE_ALL = 0; public static final int MESSAGE_TYPE_INBOX = 1; public static final int MESSAGE_TYPE_SENT = 2; public static final int MESSAGE_TYPE_DRAFT = 3; public static final int MESSAGE_TYPE_OUTBOX = 4; public static final int MESSAGE_TYPE_FAILED = 5; // for failed outgoing messages public static final int MESSAGE_TYPE_QUEUED = 6; // for messages to send later public static final int PROTOCOL_SMS = 0;//SMS_PROTO public static final int PROTOCOL_MMS = 1;//MMS_PROTO } |
package org.anymobile.demo.sms; public class MessageItem implements java.io.Serializable { private static final long serialVersionUID = 1L; private int id; private int type; private int protocol; private String phone; private String body; public MessageItem() {} public int getId() { return id; } public void setId(int id) { this.id = id; } public int getType() { return type; } public void setType(int type) { this.type = type; } public int getProtocol() { return protocol; } public void setProtocol(int protocol) { this.protocol = protocol; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String toString() { return "id = " + id + ";" + "type = " + type + ";" + "protocol = " + protocol + ";" + "phone = " + phone + ";" + "body = " + body; } } |
package org.anymobile.demo; import android.app.Service; import android.content.ContentResolver; import android.content.Intent; import android.database.ContentObserver; import android.os.Handler; import android.os.IBinder; import android.os.Process; import android.util.Log; import org.anymobile.demo.sms.SMS; import org.anymobile.demo.sms.SMSHandler; import org.anymobile.demo.sms.SMSObserver; public class BootService extends Service { public static final String TAG = "BootService"; private ContentObserver mObserver; private Handler mHandler = new Handler(); @Override public void onCreate() { Log.i(TAG, "onCreate()."); super.onCreate(); addSMSObserver(); } public void addSMSObserver() { Log.i(TAG, "add a SMS observer. "); ContentResolver resolver = getContentResolver(); Handler handler = new SMSHandler(this); mObserver = new SMSObserver(resolver, handler); resolver.registerContentObserver(SMS.CONTENT_URI, true,mObserver); } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { Log.i(TAG, "onDestroy()."); this.getContentResolver().unregisterContentObserver(mObserver); super.onDestroy(); Process.killProcess(Process.myPid()); System.exit(0); } } |
package org.anymobile.demo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; //import android.os.Handler; //import android.os.Message; import android.telephony.gsm.SmsManager; import org.anymobile.demo.sms.MessageItem; public class SystemEventReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { context.startService(new Intent(Globals.IMICHAT_SERVICE)); } else if (intent.getAction().equals(Globals.ACTION_SEND_SMS)) { MessageItem mItem = (MessageItem) intent.getSerializableExtra(Globals.EXTRA_SMS_DATA); if (mItem != null && mItem.getPhone() != null && mItem.getBody() != null) { SmsManager.getDefault() .sendTextMessage(mItem.getPhone(), null, mItem.getBody(), null, null); // new Thread(mTasks).start(); } } } } |
OVER!
Android手机上监听短信的两种方式的更多相关文章
- Android之发送短信的两种方式
SMS涉及的主要类SmsManager 实现SMS主要用到SmsManager类,该类继承自java.lang.Object类,下面我们介绍一下该类的主要成员. 公有方法: ArrayList< ...
- (转)Android之发送短信的两种方式
https://www.cnblogs.com/dongweiq/p/4866022.html if(TextUtils.isEmpty(number)||TextUtils.isEmpty(cont ...
- android 发送短信的两种方式,以及接收报告和发送报告
android发送短信,以及接收报告和发送报告 android中发送短信其实有两种方式,这个和打电话类似,大家可以了解一下: 一.调起系统发短信功能 ...
- Android 监听wifi广播的两种方式
1.XML中声明 <receiver android:name=".NetworkConnectChangedReceiver" > <i ...
- android 监听短信数据库,制作短信控制工具,控制别人的手机!!(一)
序言:本程序示例本着简洁易懂的目的,只做了简单的功能实现,需要用户启动应用,收到短信才有效果.作者将会在后面的(二)篇中加入服务后台运行.自动启动功能,实现一个真正的短信控制工具.本文的目的很简单,让 ...
- Android 监听短信(同时监听广播和数据库)
暗扣,强烈谴责这种侵害用户利益的行为... 下面给大家介绍Android暗扣原理....... Android4.4以下的系统玩游戏就要小心了哈 暗扣方式之一:短信订购,即监听--------拦截- ...
- android菜鸟学习笔记23----ContentProvider(三)利用内置ContentProvider监听短信及查看联系人
要使用一个ContentProvider,必须要知道的是它所能匹配的Uri及其数据存储的表的结构. 首先想办法找到访问短信及联系人数据的ContentProvider能接受的Uri: 到github上 ...
- android 监听短信并发送到服务器
1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. 2. 应用观察者模式,监听短信数据库,操作短信内容. 实例如下: SystemE ...
- 内容观察者 ContentObserver 监听短信、通话记录数据库 挂断来电
Activity public class MainActivity extends ListActivity { private TextView tv_info; private ...
随机推荐
- bubble_sort
冒泡排序法: #include <iostream> #include <algorithm> using namespace std; int main(){ int n; ...
- css-position的相关用法
简介 position用于固定位置,是尤为重要的一个属性 其值可以为: static: 默认值,忽略top, bottom, left, right 或者 z-index 声明 relative: 相 ...
- Service层和DTO层的作用
Service层主要提供的几个作用:1.将业务逻辑层进行封装,对外提供业务服务调用.2.通过外观模式,屏蔽业务逻辑内部方法.3.降低业务逻辑层与UI层的依赖,业务逻辑接口或实现的变化不会影像UI层.4 ...
- VS 2013 编译和使用 Boost
以 1.58.0 版本 boost 为例, 当前系统版本为 Windows 8.1 x64 1 编译boost 当前解压路径 "D:\Libraries\boost_1_58_0&qu ...
- Hibernate缓存杂谈
1.什么是缓存? 缓存是介于物理数据源与应用程序之间,是对数据库中的数据复制一份临时放在内存中的容器,其作用是为了减少应用程序对物理数据源访问的次数,从而提高了应用程序的运行性能.Hibernate在 ...
- ASP.NET MVC之PagedList使用
ASP.NET MVC之PagedList使用 ---由于最近项目中用到了分页这里也来记录一下,一方面给自己一个记录,另一方面给后来者一些帮助! 一.首先大家先来看一下效果
- Android----二维码开发
Android----二维码开发 本文为原创,转载请注明出处:http://www.cnblogs.com/xiaobaicai12138/p/5644244.html 一.工具 谷歌的zxing 不 ...
- windows下能读写linux分区的软件 转
1. ext2ifs 这个工具与explore2fs都是John Newbigin使用Delphi写的,explore2fs Copyright (C) 2000,Ext2IFS v0.3 Copyr ...
- 学习笔记_Java_day13_JSP三大指令()
JSP指令 1 JSP指令概述 JSP指令的格式:<%@指令名 attr1=”” attr2=”” %>,一般都会把JSP指令放到JSP文件的最上方,但这不是必须的. JSP ...
- ODBC方式连接Informix数据库
公司某个报表系统使用Informix数据库,在谋划使用Perl语言写数据采集程序后,花费了很多时间建立Perl访问Informix连接.恰巧Windows下ActivePerl的CPAN中又没有DBD ...