Android6.0 源碼修改之屏蔽系統(tǒng)短信功能和來電功能

一、屏蔽系統(tǒng)短信功能

1、屏蔽所有短信

android 4.2 短信發(fā)送流程分析可參考這篇 戳這

源碼位置 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即可,不去解析和分發(fā)短信消息,同時這樣操作 短信將不會記錄到短信數(shù)據(jù)庫中,插入短信消息到數(shù)據(jù)庫的方法見下文insertMessage()方法。

2、屏蔽特定的短信(特定的短信號碼或者短信內(nèi)容)

源碼位置同上

  • SmsMessage.getOriginatingAddress() 獲取短信號碼
  • SmsMessage.getMessageBody() 獲取短信內(nèi)容
    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()) || "話費(fèi)".contains(tmpsms.getMessageBody())) {
            Log.i("SmsReceived", "handleSmsReceived");
            return;
        }
        //2018-10-09 cczheng  add for intercept mms notifications  end

        ....
    }

是否插入短信消息到數(shù)據(jù)庫,insertMessage()方法在handleSmsReceived()中調(diào)用

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、應(yīng)用層攔截短信(不用修改android源碼,原理就是用你的app去替代系統(tǒng)默認(rèn)的短信app,過程略繁瑣)

需要添加SmsReceiver,MmsReceiver,ComposeSmsActivity,HeadlessSmsSendService這幾個類,并在AndroidManifest中進(jìn)行相應(yīng)配置,具體流程可參考這篇 戳這

二、屏蔽系統(tǒng)來電響鈴和通知提示

屏蔽系統(tǒng)來電可分為三個步驟

1.來電靜音,不響鈴

2.來電掛斷,不出現(xiàn)IncallActivity

3、攔截未接來電通知,不顯示在狀態(tài)欄StatusBar中

ps:此種修改方式的弊端在于來電時網(wǎng)絡(luò)數(shù)據(jù)會離線2s左右

好,現(xiàn)在我們開始按這三個步驟來修改源碼

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.來電掛斷,不出現(xiàn)IncallActivity

思路:監(jiān)聽PhoneState,當(dāng)監(jiān)聽到響鈴時,直接通過反射調(diào)用endcall方法掛斷電話。監(jiān)聽PhoneStateListener可以寫到廣播中,當(dāng)收到開機(jī)廣播時,開始監(jiān)聽phoneState,這樣和系統(tǒng)保持同步。以下是參考代碼

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.攔截未接來電通知,不顯示在狀態(tài)欄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();
    }

  ...
}

其實(shí)核心方法就是showNotification(call),發(fā)送通知當(dāng)statusBar收到通知就處理并顯示在狀態(tài)欄。

當(dāng)你發(fā)現(xiàn)這樣處理完后,重新mm,然后push替換Dialer.apk重啟后,你會坑爹的發(fā)現(xiàn)狀態(tài)欄那個未接來電圖標(biāo)依舊顯示,無fa可說,繼續(xù)跟蹤日志揪出罪魁禍?zhǔn)祝罱K發(fā)現(xiàn)另一處奇葩的地方。

源碼位置 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,這樣我們就搞定了來電功能。

三、隱藏短信應(yīng)用和電話應(yīng)用在launcher中顯示(去除AndroidManifest中的category)

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

四、總結(jié)

Android源碼修改沒有大家想象的那么難,畢竟Google工程師都已經(jīng)給我們標(biāo)注了詳細(xì)的注釋說明,只是框架和封裝的思路難以理解,這就考驗(yàn)我們的耐心了,Log是個好東西,多加日志,多分析,這樣很容易上手。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,872評論 25 709
  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 13,935評論 2 59
  • 真正靠譜的人,都有這3種能力 一個人之所以靠譜,往往是因?yàn)?種能力: 高效的閉環(huán)溝通能力、有坑必填的執(zhí)行能力、抗打...
    飛軒皓閱讀 198評論 0 0
  • 2017-10-16 姓名:李義 公司:慈溪創(chuàng)鑫車輛零部件有限公司 組別:259期利他二組 【知~學(xué)習(xí)】 背誦 六...
    六度輪回閱讀 196評論 0 0
  • 哈嘍 我仿佛聽到誰在呼喚我 嗯嗯,沒錯 我就是小群子姐 你們有沒有想我? 今天跟大家聊聊【中國式“奢婚”】 不知道...
    三分鐘資訊閱讀 325評論 0 0

友情鏈接更多精彩內(nèi)容