Android6.0 源码修改之屏蔽系统短信功能和来电功能

一、屏蔽系统短信功能 1、屏蔽所有短信

android 4.2 短信发送流程分析可参考这篇 戳这

源码位置 vendor\mediatek\proprietary\packages\apps\Mms\src\com\android\mms\transaction\SmsReceiverService.java

private void handleSmsReceived(Intent intent, int error) { //2018-10-09 cczheng add for intercept mms notifications start if (true) { Log.i("SmsReceived", "handleSmsReceived"); return; } //2018-10-09 cczheng add for intercept mms notifications end SmsMessage[] msgs = Intents.getMessagesFromIntent(intent); /// M:Code analyze 022, check null @{ if (msgs == null) { MmsLog.e(MmsApp.TXN_TAG, "getMessagesFromIntent return null."); return; } MmsLog.d(MmsApp.TXN_TAG, "handleSmsReceived SmsReceiverService"); /// ...... }

在handleSmsReceived()方法中直接return即可,不去解析和分发短信消息,同时这样操作 短信将不会记录到短信数据库中,插入短信消息到数据库的方法见下文insertMessage()方法。

2、屏蔽特定的短信(特定的短信号码或者短信内容)

源码位置同上

SmsMessage.getOriginatingAddress() 获取短信号码

SmsMessage.getMessageBody() 获取短信内容

private void handleSmsReceived(Intent intent, int error) { SmsMessage[] msgs = Intents.getMessagesFromIntent(intent); ..... /// M:Code analyze 024, print log @{ SmsMessage tmpsms = msgs[0]; MmsLog.d(MmsApp.TXN_TAG, "handleSmsReceived" + (tmpsms.isReplace() ? "(replace)" : "") + " messageUri: " + messageUri + ", address: " + tmpsms.getOriginatingAddress() + ", body: " + tmpsms.getMessageBody()); /// @ //2018-10-09 cczheng add for intercept mms notifications start if ("10010".equals(tmpsms.getOriginatingAddress()) || "话费".contains(tmpsms.getMessageBody())) { Log.i("SmsReceived", "handleSmsReceived"); return; } //2018-10-09 cczheng add for intercept mms notifications end .... }

是否插入短信消息到数据库,insertMessage()方法在handleSmsReceived()中调用

private Uri insertMessage(Context context, SmsMessage[] msgs, int error, String format) { // Build the helper classes to parse the messages. if (msgs == null) { MmsLog.e(MmsApp.TXN_TAG, "insertMessage:getMessagesFromIntent return null."); return null; } /// @} SmsMessage sms = msgs[0]; if (sms.getMessageClass() == SmsMessage.MessageClass.CLASS_0) { MmsLog.d(MmsApp.TXN_TAG, "insertMessage: display class 0 message!"); displayClassZeroMessage(context, msgs, format); return null; } else if (sms.isReplace()) { MmsLog.d(MmsApp.TXN_TAG, "insertMessage: is replace message!"); return replaceMessage(context, msgs, error); } else { MmsLog.d(MmsApp.TXN_TAG, "insertMessage: stored directly!"); return storeMessage(context, msgs, error); } } 3、应用层拦截短信(不用修改android源码,原理就是用你的app去替代系统默认的短信app,过程略繁琐)

需要添加SmsReceiver,MmsReceiver,ComposeSmsActivity,HeadlessSmsSendService这几个类,并在AndroidManifest中进行相应配置,具体流程可参考这篇 戳这

二、屏蔽系统来电响铃和通知提示

屏蔽系统来电可分为三个步骤

1.来电静音,不响铃

2.来电挂断,不出现IncallActivity

3、拦截未接来电通知,不显示在状态栏StatusBar中

ps:此种修改方式的弊端在于来电时网络数据会离线2s左右

好,现在我们开始按这三个步骤来修改源码

1.来电静音,不响铃

源码位置 packages/services/Telecomm/src/com/android/server/telecom/Ringer.java

private void updateRinging(Call call) { if (mRingingCalls.isEmpty()) { stopRinging(call, "No more ringing calls found"); stopCallWaiting(call); } else { //2018-10-10 cczheng add anotation function startRingingOrCallWaiting() for silent call start Log.d("callRinging", "silent call, will not play ringtone"); // startRingingOrCallWaiting(call); //2018-10-10 cczheng add anotation function startRingingOrCallWaiting() for silent call end } }

是的,注释掉startRingingOrCallWaiting(call);方法就ok啦

2.来电挂断,不出现IncallActivity

思路:监听PhoneState,当监听到响铃时,直接通过反射调用endcall方法挂断电话。监听PhoneStateListener可以写到广播中,当收到开机广播时,开始监听phoneState,这样和系统保持同步。以下是参考代码

public class PhoneStartReceiver extends BroadcastReceiver { private static final String TAG = "PhoneStartReceiver"; private PhoneCallListener mPhoneCallListener; private TelephonyManager mTelephonyManager; @Override public void onReceive(final Context context, final Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { // endCall when CALL_STATE_RINGING initPhoneCallListener(context); } } private void initPhoneCallListener(Context context){ mPhoneCallListener = new PhoneCallListener(); mTelephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); mTelephonyManager.listen(mPhoneCallListener, PhoneCallListener.LISTEN_CALL_STATE); } public class PhoneCallListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { Log.v(TAG, "onCallStateChanged-state: " + state); Log.v(TAG, "onCallStateChanged-incomingNumber: " + incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_RINGING: endCall(); break; default: break; } super.onCallStateChanged(state, incomingNumber); } } private void endCall() { try { Method m1 = mTelephonyManager.getClass().getDeclaredMethod("getITelephony"); if (m1 != null) { m1.setAccessible(true); Object iTelephony = m1.invoke(mTelephonyManager); if (iTelephony != null) { Method m2 = iTelephony.getClass().getDeclaredMethod("silenceRinger"); if (m2 != null) { m2.invoke(iTelephony); Log.v(TAG, "silenceRinger......"); } Method m3 = iTelephony.getClass().getDeclaredMethod("endCall"); if (m3 != null) { m3.invoke(iTelephony); Log.v(TAG, "endCall......"); } } } } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "endCallError", e); } } } 3.拦截未接来电通知,不显示在状态栏StatusBarr中

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwsxjp.html