检测传入和传出的短信?

Detecting incoming and outgoing sms?

通过 ContentObserver 检测传入和传出的 SMS 或使用 BroadCast Receiver 检测传入的 SMS 和 ContentObserver 检测传出的 SMS 是好习惯吗?

我有一个关于 SMS 和 Call blocker 的应用程序,但我从未将它上传到 Play 商店,因为在 Kitkat 之后,你无法阻止短信。如果你想那样做,那就别这样。我只分享短信部分,大家可以随意修改

<!-- Android Permissions in manifest -->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

<!-- Define receiver inside the application tag -->
<receiver android:name="catchPackage.SmsController" android:process=":hascode_process" android:label="@string/sms_receiver"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter android:priority="999">
            <action android:name="android.provider.Telephony.SMS_DELIVER"/>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>
<!-- Broadcast receiver java code -->

 public void onReceive(Context context, Intent intent) {
    this.context = context;
          try{
              Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
                SmsMessage[] msgs = null;
                String msg_from = null;
                String msg_body = null;
                if (bundle != null){
                    try{

                        boolean delete= false;
                        Object[] pdus = (Object[]) bundle.get("pdus");
                        msgs = new SmsMessage[pdus.length];
                        for(int k=0; k<msgs.length; k++){
                            msgs[k] = SmsMessage.createFromPdu((byte[])pdus[k]);
                            msg_from = msgs[k].getOriginatingAddress();
                            System.out.println(msg_from);
                            String msgBody = msgs[k].getMessageBody();
                            msg_body = msgBody;


                        }

                    }catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                }
            } catch (Exception e) {
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
    }

}