本文主要是介绍在无需授权的情况下,通过Xposed实现短信的监听,附加发送短信以及发送状态监听,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近在开发中有这样的一个需求,拦截手机中收到的所有短信进行记录,特此记录一下实现代码。
Class<?> mSmsMessageClass = XposedHelpers.findClass("com.android.internal.telephony.gsm.SmsMessage", classLoader);XposedHelpers.findAndHookMethod(mSmsMessageClass, "createFromPdu", byte[].class,new XC_MethodHook() {@Overrideprotected void afterHookedMethod(MethodHookParam param)throws Throwable {// TODO Auto-generated method stubtry {Object smsMessage = param.getResult();if (null != smsMessage) {String from = (String) XposedHelpers.callMethod(smsMessage, "getOriginatingAddress");String msgBody = (String) XposedHelpers.callMethod(smsMessage, "getMessageBody");XLog.e("test_sms", "收到短信---->" + "from:" + from + " msgBody:" + msgBody);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();XLog.e("SMS listen error", e.getMessage());}}});
以下为发送短信的实现代码
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;import java.util.ArrayList;/*** createTime: 2019/7/16.17:28* updateTime: 2019/7/16.17:28* author: singleMan.* desc: 发送短信的class*/public class SMSUtils {//短信发送状态public static final String SENT_SMS_ACTION = "SENT_SMS_ACTION";//短信发送结果public static final String DELIVERED_SMS_ACTION = "SMS_DELIVERED_ACTION";//目标手机号码public static final String KEY_PHONENUM = "phonenumber";//短信消息IDpublic static final String MSG_ID = "msg_id";/*** 发送短信** @param msgEntity* @return*/public static boolean SendSMS(Context context, SMSEntity msgEntity, int index) {if (null == msgEntity) {return false;}if (TextUtils.isEmpty(msgEntity.getMobile()) || TextUtils.isEmpty(msgEntity.getMsgTxt())) {return false;}//如果短信内容大于70if (msgEntity.getMsgTxt().length() > 70) {return SendLongMessage(context, msgEntity, index);}try {Intent intent = new Intent(SENT_SMS_ACTION);intent.putExtra(KEY_PHONENUM, msgEntity.getMobile());intent.putExtra(MSG_ID, msgEntity.getId());PendingIntent pSendIntent = PendingIntent.getBroadcast(context, index, intent, PendingIntent.FLAG_ONE_SHOT);Intent deliveryIntent = new Intent(DELIVERED_SMS_ACTION);deliveryIntent.putExtra(KEY_PHONENUM, msgEntity.getMobile());deliveryIntent.putExtra(MSG_ID, msgEntity.getId());PendingIntent pDeliveryIntent = PendingIntent.getBroadcast(context, index, deliveryIntent, PendingIntent.FLAG_ONE_SHOT);android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();smsManager.sendTextMessage(msgEntity.getMobile(), null, msgEntity.getMsgTxt(), pSendIntent, pDeliveryIntent);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 内容超过70的短信将被分为两条短信进行发送* @param context* @param msgEntity* @param index* @return*/private static boolean SendLongMessage(Context context, SMSEntity msgEntity, int index) {ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>();android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();try {Intent intent = new Intent(SENT_SMS_ACTION);intent.putExtra(KEY_PHONENUM, msgEntity.getMobile());intent.putExtra(MSG_ID, msgEntity.getId());PendingIntent pSendIntent = PendingIntent.getBroadcast(context, index, intent, PendingIntent.FLAG_ONE_SHOT);Intent deliveryIntent = new Intent(DELIVERED_SMS_ACTION);deliveryIntent.putExtra(KEY_PHONENUM, msgEntity.getMobile());deliveryIntent.putExtra(MSG_ID, msgEntity.getId());PendingIntent pDeliveryIntent = PendingIntent.getBroadcast(context, index, deliveryIntent, PendingIntent.FLAG_ONE_SHOT);ArrayList<String> mSMSMessage = smsManager.divideMessage(msgEntity.getMsgTxt());for (int i = 0; i < mSMSMessage.size(); i++) {sentPendingIntents.add(i, pSendIntent);deliveredPendingIntents.add(i, pDeliveryIntent);}/* 发送SMS短信,注意倒数的两个PendingIntent参数 */smsManager.sendMultipartTextMessage(msgEntity.getMobile(), null, mSMSMessage, sentPendingIntents, deliveredPendingIntents);return true;} catch (Exception e) {e.printStackTrace();}return false;}}
附上发送方法中用到的Model类
/*** createTime: 2019/7/16.17:43* updateTime: 2019/7/16.17:43* author: singleMan.* desc: 一条短息*/public class SMSEntity {String id;String mobile;String msgTxt;public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}public String getMsgTxt() {return msgTxt;}public void setMsgTxt(String msgTxt) {this.msgTxt = msgTxt;}public String getId() {return id;}public void setId(String id) {this.id = id;}}
接下来是在广播中监听短信的发送状态,并进行相应的处理
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.text.TextUtils;/*** createTime: 2019/7/16.17:29* updateTime: 2019/7/16.17:29* author: singleMan.* desc: 状态监听广播*/public class SmsStateListenerReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();String phoneNumber = intent.getStringExtra(SMSUtils.KEY_PHONENUM);String msgId = intent.getStringExtra(SMSUtils.MSG_ID);XLog.e("SMS-receiver", "短信的广播中心:" + action + "--->" + msgId + ":" + phoneNumber);//是否已发送if (TextUtils.equals(action, SMSUtils.SENT_SMS_ACTION)) {switch (getResultCode()) {case Activity.RESULT_OK://TODO 上报已经发送但是不确定是否成功XLog.e("SMS-receiver", "已发送");reportSendMsgStatus(msgId, true, "已发送完成");break;case SmsManager.RESULT_ERROR_GENERIC_FAILURE:case SmsManager.RESULT_ERROR_RADIO_OFF:case SmsManager.RESULT_ERROR_NULL_PDU:default://TODO 上报发送失败XLog.e("SMS-receiver", "发送失败");reportSendMsgStatus(msgId, false, "发送失败");
// sendMessageToView(2, msgId);break;}}//是否已送达else if (TextUtils.equals(action, SMSUtils.DELIVERED_SMS_ACTION)) {switch (getResultCode()) {case Activity.RESULT_OK://TODO 上报已经送达XLog.e("SMS-receiver", "已送达");reportSendMsgStatus(msgId, true, "短信已送达");break;case SmsManager.RESULT_ERROR_GENERIC_FAILURE:case SmsManager.RESULT_ERROR_RADIO_OFF:case SmsManager.RESULT_ERROR_NULL_PDU:default://TODO 上报未送达XLog.e("SMS-receiver", "未送达");reportSendMsgStatus(msgId, false, "短信未送达");break;}}}/*** 上报发送状态** @param taskId* @param isSuccess* @param message*/private void reportSendMsgStatus(String taskId, boolean isSuccess, String message) {//TODO 我这里执行的上报,可以根据需求进行自行修改}
记得注册广播呦 ! unRegisger的代码就不粘的,相信大家也不需要。
/*** 注册广播*/private void registerSMSReceiver() {//注册监听短信是否成功的广播.SmsStateListenerReceiver mSMSStateListenerReceiver = new SmsStateListenerReceiver();IntentFilter mFilter = new IntentFilter();mFilter.addAction(SMSUtils.SENT_SMS_ACTION);mFilter.addAction(SMSUtils.DELIVERED_SMS_ACTION);registerReceiver(mSMSStateListenerReceiver, mFilter);}
特此记录发送短信以及监听发送状态的实现。以及通过Xposed进行短信的监听。
这篇关于在无需授权的情况下,通过Xposed实现短信的监听,附加发送短信以及发送状态监听的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!