一、屏蔽系统短信功能

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中

源码位置 packages/apps/InCallUI/src/com/android/incallui/StatusBarNotifier.java

private void updateInCallNotification(final InCallState state, CallList callList) {
... final Call call = getCallToShow(callList); //2018-10-10 cczheng add intercept incoming notification start
if (true) {
if (call != null) {
Log.v("InCallNotification", "phoneNumber = " + call.getNumber());
}
return;
}
//2018-10-10 cczheng add intercept incoming notification end if (call != null) {
showNotification(call);
} else {
cancelNotification();
} ...
}

其实核心方法就是showNotification(call),发送通知当statusBar收到通知就处理并显示在状态栏。

当你发现这样处理完后,重新mm,然后push替换Dialer.apk重启后,你会坑爹的发现状态栏那个未接来电图标依旧显示,无fa可说,继续跟踪日志揪出罪魁祸首,最终发现另一处奇葩的地方。

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

诺,就是这了,看注释就明白了吧 Create a system notification for the missed call

/**
* Create a system notification for the missed call.
*
* @param call The missed call.
*/
@Override
public void showMissedCallNotification(Call call) {
////2018-10-10 cczheng hide missed call notification [S]
if (true) {
android.util.Log.i("misscall", "showMissedCallNotification......");
return;
}
///2018-10-10 cczheng hide missed call notification [E]
mMissedCallCount++; final int titleResId;
final String expandedText; // The text in the notification's line 1 and 2.
....
}

ok,这样我们就搞定了来电功能。

三、隐藏短信应用和电话应用在launcher中显示(去除AndroidManifest中的category)

<category android:name="android.intent.category.LAUNCHER" />

四、总结

Android源码修改没有大家想象的那么难,毕竟Google工程师都已经给我们标注了详细的注释说明,只是框架和封装的思路难以理解,这就考验我们的耐心了,Log是个好东西,多加日志,多分析,这样很容易上手。


Android6.0 源码修改之屏蔽系统短信功能和来电功能的更多相关文章

  1. Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar

    场景分析, 为了完全实现沉浸式效果,在进入特定的app后可以将导航栏移除,当退出app后再次将导航栏恢复.(下面将采用发送广播的方式来移除和恢复导航栏) ps:不修改源码的情况下,简单的沉浸式效果实现 ...

  2. Android6.0 源码修改之 仿IOS添加全屏可拖拽浮窗返回按钮

    前言 之前写过屏蔽系统导航栏功能的文章,具体可看Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar 在某些特殊定制的版本中要求 ...

  3. Android6.0 源码修改之 Contacts应用

    一.Contacts应用的主界面和联系人详情界面增加顶部菜单添加退出按钮 通过Hierarchy View 工具可以发现 主界面对应的类为 PeopleActivity 联系人详情界面对应的类为 Qu ...

  4. Android6.0 源码修改之Setting列表配置项动态添加和静态添加

    写在前面 最近客户有个需求,要求增加操作Setting列表配置项的功能,是不是一脸懵,没关系,一图胜千言,接下来就上图.诺,就是这么个意思.   原来的列表配置项     增加了单个配置项     增 ...

  5. Android6.0 源码修改之Settings音量调节界面增加通话音量调节

    前言 今天客户提了个需求,因为我们的设备在正常情况下无法调节通话音量,只有在打电话过程中,按物理音量加减键才能出现调节通话音量seekBar,很不方便,于是乎需求就来了.需要优化两个地方 1.在正常情 ...

  6. 在Ubuntu Server14.04上编译Android6.0源码

    此前编译过Android4.4的源码,但是现在Android都到了7.0的版本,不禁让我感叹Google的步伐真心难跟上,趁这周周末时间比较充裕,于是在过去的24小时里,毅然花了9个小时编译了一把An ...

  7. Ubuntu16.04下编译android6.0源码

    http://blog.csdn.net/cnliwy/article/details/52189349 作为一名合格的android开发人员,怎么能不会编译android源码呢!一定要来一次说编译就 ...

  8. Android6.0源码下载编译刷入真机

    编译环境是Ubuntu12.04.手机nexus 5,编译安卓6.0.1源码并烧录到真机. 源码用的是科大的镜像:http://mirrors.ustc.edu.cn/aosp-monthly/,下载 ...

  9. Android6.0源码分析之录音功能(一)【转】

    本文转载自:http://blog.csdn.net/zrf1335348191/article/details/54949549 从现在开始一周时间研究录音,下周出来一个完整的博客,监督,激励!!! ...

随机推荐

  1. 微信小程序入门(三)

    11.开发框架基本介绍 四个组成部分,其它三个前面介绍过了,主要WXS: WXS:对wxml增强的一种脚本语言,可以对请求的数据进行filter或者做计算处理,帮助wxml快速构建出页面结构. 12. ...

  2. spring-boot-maven-plugin插件的作用

    要记住:spring-boot-maven-plugin插件在打Jar包时会引入依赖包 可以打成直接运行的Jar包 maven项目的pom.xml中,添加了org.springframework.bo ...

  3. SpringBoot配置Cors解决跨域请求问题

    一.同源策略简介 同源策略[same origin policy]是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源. 同源策略是浏览器安全的基石. 什么是源 源[or ...

  4. 面试必备技能-HiveSQL优化

    Hive SQL基本上适用大数据领域离线数据处理的大部分场景.Hive SQL的优化也是我们必须掌握的技能,而且,面试一定会问.那么,我希望面试者能答出其中的80%优化点,在这个问题上才算过关. Hi ...

  5. 了解Java内存模型,看完这一篇就够了

    前言(此文草稿是年前写的,但由于杂事甚多一直未完善好.清明假无事,便收收尾发布了) 年关将近,个人工作学习怠惰了不少.两年前刚做开发的时候,信心满满想看看一个人通过自己的努力,最终能达到一个什么样的高 ...

  6. springboot情操陶冶-@Configuration注解解析

    承接前文springboot情操陶冶-SpringApplication(二),本文将在前文的基础上分析下@Configuration注解是如何一步一步被解析的 @Configuration 如果要了 ...

  7. Python机器学习笔记 K-近邻算法

    K近邻(KNN,k-NearestNeighbor)分类算法是数据挖掘分类技术中最简单的方法之一. 所谓K最近邻,就是K个最近的邻居的意思,说的是每个样本都可以用它最接近的k个邻居来代表.KNN算法的 ...

  8. MySQL的使用及优化

    前言 最近听了公司里的同事做的技术分享,然后觉得对自己还是挺有帮助的.都是一些日常需要注意的地方,我们目前在开发过程中,其实用不到MySQL太深的内容的.只是能适用我们日常开发的知识就可以了.所以我将 ...

  9. Python中return self的用法

      在Python中,有些开源项目中的方法返回结果为self. 对于不熟悉这种用法的读者来说,这无疑使人困扰,本文的目的就是给出这种语法的一个解释,并且给出几个例子.   在Python中,retur ...

  10. C#隐藏与显示系统任务栏和开始菜单栏按钮

    隐藏与显示系统任务栏和开始菜单栏按钮:直接上代码:       private const int SW_HIDE = 0;  //隐藏       private const int SW_REST ...