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中
源码位置 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是个好东西,多加日志,多分析,这样很容易上手。
最新文章
- 阿里云linux ecs服务器配置apache+php环境
- spring @import和@importResource
- Js apply call方法详解【转】
- 三款SDR平台对比:HackRF,bladeRF和USRP
- Android 自定义属性
- Linux shell编程札记
- File.Create创建文件后,需要释放资源
- (转)操作型数据库的春天:MongoDB 1.5亿美元融资背后的故事
- C#操作Access
- do/while(0) c4127
- mvc/mvvm小小的总结
- HTML的三种布局:DIV+CSS、FLEX、GRID
- J2EE进阶(七)利用SSH框架根据数据表建立model类
- 发现了一个App拉新工具:免填邀请码
- JAVA BigDecimal 用法
- jsp 异步处理
- .net core实践系列之短信服务-Sikiro.SMS.Job服务的实现
- Redis 学习之路 (009) - Redis-cli命令最新总结
- 一些适合初学者的C/C++语言开发环境(IDE)
- 读书笔记 effective c++ Item 42 理解typename的两种涵义
热门文章
- MYSQL服务器系统变量
- python基础-变量运算符(3)
- 一道关于:ArrayList、Vector、LinkedList的存储性能和特性 的面试题
- Python快速学习01:Eclipse上配置PyDev &; &#39;Hello World !&#39;
- HashMap source code view(1)
- jvm的那些设置参数你都知道吗
- Linux常用命令之文件搜索命令
- SOAP系列目录
- 【ASP.NET MVC系列】浅谈jqGrid 在ASP.NET MVC中增删改查
- 【学习笔记】node.js重构路由功能