检测传入和传出的短信?

时间:2015-07-09 19:39:51

标签: android broadcastreceiver contentobserver

通过ContentObserver检测传入和传出短信或使用BroadCast Receiver传入短信和ContentObserver传出短信是很好的选择吗?

1 个答案:

答案 0 :(得分:0)

我有一个关于短信和呼叫拦截器的应用程序但我从未上传到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();
            }
    }

}